File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Trying to delete a string from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Trying to delete a string from a text file" Watch "Trying to delete a string from a text file" New topic
Author

Trying to delete a string from a text file

George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
By testing the code out, I hardcoded it for now to delete one item which is "tv" but it still remains in my text file.
my file consists:
Ps4
Xbox
tv
Iphone
Alienware_desktop

tv is not deleted and the tempFile was never deleted. Really nothing changed, only the addition of the tempFile.
There is no good examples out their on the internet so if anyone can help me out that would be great! The deleteItem coding is between lines 132 and 164...

E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
George Avilez wrote:By testing the code out, I hardcoded it for now to delete one item which is "tv" but it still remains in my text file.
my file consists:
Ps4
Xbox
tv
Iphone
Alienware_desktop

tv is not deleted and the tempFile was never deleted. Really nothing changed, only the addition of the tempFile.

What is in the tempfile? Is the tv line removed from the temp file?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
trimmedLine is exactly the same as currentLine, ie it's not trimmed. I suggest you change your code so it is trimmed as your file may contain spurious whitespace. You may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Also check what the value of the successful variable is after you try to rename the file. You haven't closed the reader, so the rename may be failing because of that.
George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
Alright I finally got it to somewhat work. 'tv' is deleted in the new tempFile but all the Strings are on one line. How do I keep it like it was in the gifts.txt? And none of the file names changed. Can anyone see what's wrong? DOes it have to do with the Boolean variable?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
Wrap your BufferredWriter in a PrintWriter and output the text using println() or write the appropriate newline character(s) to the writer.

BTW you should really close those streams in a finally clause or if you are using Java 7 or later then use a try-with-resources statement
George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
Wrap your BufferredWriter in a PrintWriter and output the text using println() or write the appropriate newline character(s) to the writer.

Awesome that worked like a charm! Now my only question is how do I properly rename the templFIle to "gifts" and deleting the old "gifts" file?
George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
ou may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.

That's actually something I wanted to ask also, I was going to have the user enter "_" instead of spaces (incase there gift was more than one word) because then the program will kinda be messed up when each println separates words. Example: xbox 360 will be separated when I need it on one line. So with that in mind, will ignore case not work since the underscore may be present?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
George Avilez wrote:
ou may also want to look at using equalsIgnoreCase() rather than equals unless you really need a case sensitive test.

That's actually something I wanted to ask also, I was going to have the user enter "_" instead of spaces (incase there gift was more than one word) because then the program will kinda be messed up when each println separates words. Example: xbox 360 will be separated when I need it on one line. So with that in mind, will ignore case not work since the underscore may be present?

Why would each println separate words? println just prints the text you give it and then prints the platform specific newline char(s).
To answer your question equalsIgnoreCase will match underscores in both strings but won't match underscores against space chars.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
George Avilez wrote:Now my only question is how do I properly rename the templFIle to "gifts" and deleting the old "gifts" file?

Call delete on the gifts file object and then pass the object to the tempFile object's rename() method. Remember to check the return values from these methods to ensure they have succeeded before continuing.
George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
Thanks a bunch a man! =) I have it working
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
My pleasure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to delete a string from a text file