Forums Register Login

Trying to delete a string from a text file

+Pie Number of slices to send: Send
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...

+Pie Number of slices to send: Send
 

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?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?

1
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thanks a bunch a man! =) I have it working
+Pie Number of slices to send: Send
My pleasure.
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1250 times.
Similar Threads
Need help with username and password validation
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>(); does not work
Pesudo coding
Unable to rename file
Problems w/ a HashMap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:03:11.