• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to delete a string from a text file

 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch a man! =) I have it working
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic