Read and Writing to a file simultaneously in java

15,798

Solution 1

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.

Solution 2

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.

Share:
15,798
iphonedev7
Author by

iphonedev7

Updated on June 19, 2022

Comments

  • iphonedev7
    iphonedev7 almost 2 years

    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!

  • iphonedev7
    iphonedev7 over 11 years
    Good idea...I'll try that out.
  • iphonedev7
    iphonedev7 over 11 years
    @MelNicholson Good to know. Thanks.