aspose file tools
The moose likes Java in General and the fly likes Delete a text file Containing a certain String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Delete a text file Containing a certain String" Watch "Delete a text file Containing a certain String" New topic
Author

Delete a text file Containing a certain String

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

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?


Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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

Joined: Dec 09, 2011
Posts: 155

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

Joined: Feb 07, 2012
Posts: 344

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

Joined: Dec 09, 2011
Posts: 155

I got a null pointer exception on the line number 39 where there is
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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

Joined: Dec 09, 2011
Posts: 155

All files have more than eight Lines, you can be sure about that.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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

Joined: Dec 09, 2011
Posts: 155

The email address is printed on the eighth line and if a file does not contain an email address, the eighth line is blank.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

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.


Never ascribe to malice that which can be adequately explained by stupidity.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4747
    
    7

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


Isn't it funny how there's always time and money enough to do it WRONG?
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

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

Joined: Dec 09, 2011
Posts: 155

Thanks guys for your help. fred rosenberger, I used you Idea and it worked fine. Modified from String email

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete a text file Containing a certain String
 
Similar Threads
resume parser
Split a Text File Following a Criteria.
Split a File According to a String in the File
File path issues while reading an xsl file in java
Skip Reading several Lines using BufferedReader