Forums Register Login

replacing content in a file

+Pie Number of slices to send: Send
1)
I am using this code to replace string "javascript" with "perl script" in the file. When I run this code, all the content in the profile.txt is getting erased and file becomes blank. Why is that ? How can I relace the content ?

2) Secondly, when I use "import java.util.regex;" I am getting an error.

Replacement.java:3: cannot find symbol
symbol : class regex
location: package java.util
import java.util.regex;
^


Why can I not import java.util.regex ?
+Pie Number of slices to send: Send
Because when you open a file for output, that clears any data which was in the file before you did that. So what you should do is to write to a different file from the one you're reading from. When you have finished working with the files, close them all and then delete the old file and rename the new file.

And you can't import java.util.regex because there is no such class. If you meant to import the java.util.regex package, you do that like this:
+Pie Number of slices to send: Send
Does it mean that I can not modify the existing files without creating new files ?

It will happen the same thing with new file also. Whenever new line is written to the file, all the existing content from the file will get erased. Is there a way to store all the lines in an arraylist and write entire arraylist to the file ?

with br.readLine(), I can read one line at a time and with br.read(), I can read one character at a time. How can I read entire file at once rather than reading line by line ?
+Pie Number of slices to send: Send
 

nirjari patel wrote:Does it mean that I can not modify the existing files without creating new files ?


You cannot open the same file for reading and writing at the same time and edit it in the way that you're trying to do with the code above.

nirjari patel wrote:Is there a way to store all the lines in an arraylist and write entire arraylist to the file ?

with br.readLine(), I can read one line at a time and with br.read(), I can read one character at a time. How can I read entire file at once rather than reading line by line ?


Ofcourse, you can read the entire contents of the file into for example an ArrayList in memory, then change the content of the ArrayList, and then write it back. There's no easy, built-in way to read in the file with a single statement. But reading or writing a file line by line is not so hard. Do something like this:

  • First read the file: open it for reading, use a BufferedReader to read all the lines in a loop (calling readLine() on the BufferedReader until it returns null).
  • Then edit the text in the ArrayList.
  • Then write the file: open it for writing, use a PrintWriter to write all the lines in a loop.


  • Try writing some code to do that and let us know how it works out!
    +Pie Number of slices to send: Send
    I tried but its not working. I am puttiunng the code in here. Please let me know what needs to be corrected.
    +Pie Number of slices to send: Send


    There is no write() method that takes an array list, you need to iterate through the list and write all the strings in the list.

    Henry
    +Pie Number of slices to send: Send
    So this is like reading one line at a time and printing it back to the file. But when I execute this code the output file is blank. What should I do in the code to get output in the file ? Can you please provide me with a sample code, how to read from a file, make change in the content and write the whole file to a new file ?

    Thanks
    +Pie Number of slices to send: Send
     

    nirjari patel wrote: But when I execute this code the output file is blank. What should I do in the code to get output in the file ? Can you please provide me with a sample code, how to read from a file, make change in the content and write the whole file to a new file ?



    I think it may be a better idea to figure out what is wrong first, you can't fix when you don't know what is broken.

    I recommend that you print the exception -- assuming your catch caught one -- than to just catch it silently. That would be my guess why nothing happens. If that doesn't isolate it, then I recommend putting debugging messages to figure out where it went.

    Henry
    You've gotta fight it! Don't give in! Read this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 51379 times.
    Similar Threads
    Create .txt file
    FileIO
    how to see how control is moving in a java program during execution
    error with printing ArrayList elements
    reverse the text file and write
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 01:42:27.