• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Delete a text file Containing a certain String

 
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys, I am Reading a number of text files in directory for sending as attachments. Apparently, I am only interested with the text files that contain an e-mail address on the eighth Line. If the text file does not contain a valid address or the line number 8 is null, it should be deleted. Am using this piece of code that does not seem to be working. Please Help.



Where Could I be getting it wrong?
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stanley Mungai wrote:this piece of code that does not seem to be working.


"not working" is very vague. Be specific. What is not working? Errors? Stacktraces? Any other details?
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it does not delete the files that do not contain an email address on the eighth line or a null line in the eighth line.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not closing your files. After "String email = br.readLine();" you should "br.close()" before you can delete it.

Additionally (although not related to your problem), how are you handling files that have less than 8 lines? Or what if the 8th line has only spaces in it?
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a null pointer exception on the line number 39 where there is
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stanley Mungai wrote:I got a null pointer exception on the line number 39 where there is


That may happen when your file has less than 8 lines. You're not checking for null values. br.readLine() returns null if there is nothing more to read.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All files have more than eight Lines, you can be sure about that.
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stanley Mungai wrote:All files have more than eight Lines, you can be sure about that.


Your code would be more robust if it won't crash anyway, by being able to handle file with less than 8 lines, even if it "never" happens. In that case you could just delete the file without having to check for an email address.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The email address is printed on the eighth line and if a file does not contain an email address, the eighth line is blank.
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are doing too much at once. you should start over.

Write the code that does nothing more than prints the name of all the files you want to search.

once that works, write code to read and print the 8th line.

once that works, write code that only prints the 8th line of files that meet your criteria.

once that works, write code that lists the name of the files that meet your criteria.

once that works, write the code to delete the list of files.

With the code you have, you don't know if the problem is finding the correct files, deleting the correct files, or something else.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Apparently, I am only interested with the text files that contain an e-mail address on the eighth Line.


Apparently? Does that mean the directory could contain files that aren't text files?

The only way I know to do that is to check the 'magic number'; but I have no idea if it works on any system but Unix/Linux - and I also don't know if there's any equivalent for Java (I'd doubt it, unless it's a cross-platform solution).

It has to be said that the chances of a non-text file read as text is highly unlikely to produce a match.

Winston
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Winston, All the files are text files but some do not have email addresses on the eighth line and some contains an invalid email address on the eighth line. Those are the files I want to delete.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your help. fred rosenberger, I used you Idea and it worked fine. Modified from String email

 
I think she's lovely. It's this tiny ad that called her crazy:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic