aspose file tools*
The moose likes Beginning Java and the fly likes replacing content in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replacing content in a file" Watch "replacing content in a file" New topic
Author

replacing content in a file

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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 ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

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

Joined: Apr 23, 2009
Posts: 374
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 ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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!

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    nirjari patel
    Ranch Hand

    Joined: Apr 23, 2009
    Posts: 374
    I tried but its not working. I am puttiunng the code in here. Please let me know what needs to be corrected.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19073
        
      40



    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    nirjari patel
    Ranch Hand

    Joined: Apr 23, 2009
    Posts: 374
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19073
        
      40

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: replacing content in a file