• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

replacing content in a file

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 28416
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
     
    nirjari patel
    Ranch Hand
    Posts: 386
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I tried but its not working. I am puttiunng the code in here. Please let me know what needs to be corrected.
     
    author
    Posts: 23959
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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
     
    nirjari patel
    Ranch Hand
    Posts: 386
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Henry Wong
    author
    Posts: 23959
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic