Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and Writing to a file simultaneously in java

I'm reading a file line by line, and I am trying to make it so that if I get to a line that fits my specific parameters (in my case if it begins with a certain word), that I can overwrite that line.

My current code:

try {
    FileInputStream fis = new FileInputStream(myFile);
    DataInputStream in = new DataInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line;

    while ((line = br.readLine()) != null) {
        System.out.println(line);
            if (line.startsWith("word")) {
                // replace line code here
            }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

...where myFile is a File object.

As always, any help, examples, or suggestions are much appreciated.

Thanks!

like image 885
iphonedev7 Avatar asked Jan 23 '13 00:01

iphonedev7


People also ask

Can you read and write to the same file in Java?

You can't open the same file to read and write at the same time. You have to open and save the file information in a data structure and then close it. Then you have to work with the data structure in memory and open the file to write results. And when you finish to write you should close it.

How read and write data to a file in Java?

Java FileWriter and FileReader classes are used to write and read data from text files (they are Character Stream classes). It is recommended not to use the FileInputStream and FileOutputStream classes if you have to read and write any textual information as these are Byte stream classes.

Can we read write a stream simultaneously in Java?

We can use what we have learned to read from one file and simultaneously write to another, as demonstrated in Listing C. The Java I/O facilities add a simple and standardized API for reading and writing character and byte data from various data sources.

Which class is used to read and write bytes in a file?

 OutputStream Classes - These classes are subclasses of an abstract class, OutputStream and they are used to write bytes to a destination(file, memory or console). InputStream class is a base class of all the classes that are used to read bytes from a file, memory or console.


2 Answers

RandomAccessFile seems a good fit. Its javadoc says:

Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.

That said, since text files are a sequential file format, you can not replace a line with a line of a different length without moving all subsequent characters around, so to replace lines will in general amount to reading and writing the entire file. This may be easier to accomplish if you write to a separate file, and rename the output file once you are done. This is also more robust in case if something goes wrong, as one can simply retry with the contents of the initial file. The only advantage of RandomAccessFile is that you do not need the disk space for the temporary output file, and may get slight better performance out of the disk due to better access locality.

like image 85
meriton Avatar answered Oct 15 '22 10:10

meriton


Your best bet here is likely going to be reading in the file into memory (Something like a StringBuilder) and writing what you want your output file to look like into the StringBuilder. After you're done reading in the file completely, you'll then want to write the contents of the StringBuilder to the file.

If the file is too large to accomplish this in memory you can always read in the contents of the file line by line and write them to a temporary file instead of a StringBuilder. After that is done you can delete the old file and move the temporary one in its place.

like image 26
Marc Baumbach Avatar answered Oct 15 '22 10:10

Marc Baumbach