Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting text into an existing file via Java

I would like to create a simple program (in Java) which edits text files - particularly one which performs inserting arbitrary pieces of text at random positions in a text file. This feature is part of a larger program I am currently writing.

Reading the description about java.util.RandomAccessFile, it appears that any write operations performed in the middle of a file would actually overwrite the exiting content. This is a side-effect which I would like to avoid (if possible).

Is there a simple way to achieve this?

Thanks in advance.

like image 371
Dinuk Avatar asked Nov 14 '08 12:11

Dinuk


People also ask

How do I add text to an existing file in Java?

Likewise, the text to be added is stored in the variable text . Then, inside a try-catch block we use Files ' write() method to append text to the existing file. The write() method takes the path of the given file, the text to the written, and how the file should be open for writing.

How do I add data to an existing file?

Using the >> operator will append data at the end of the file, while using the > will overwrite the contents of the file if already existing. Now when you try to do echo "hello" > file. txt you will get a warning saying cannot overwrite existing file .

How do I edit an existing file in Java?

As proposed in the accepted answer to a similar question: open a temporary file in writing mode at the same time, and for each line, read it, modify if necessary, then write into the temporary file. At the end, delete the original and rename the temporary file.


2 Answers

Okay, this question is pretty old, but FileChannels exist since Java 1.4 and I don't know why they aren't mentioned anywhere when dealing with the problem of replacing or inserting content in files. FileChannels are fast, use them.

Here's an example (ignoring exceptions and some other stuff):

public void insert(String filename, long offset, byte[] content) {   RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");   RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");   long fileSize = r.length();   FileChannel sourceChannel = r.getChannel();   FileChannel targetChannel = rtemp.getChannel();   sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);   sourceChannel.truncate(offset);   r.seek(offset);   r.write(content);   long newOffset = r.getFilePointer();   targetChannel.position(0L);   sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));   sourceChannel.close();   targetChannel.close(); } 
like image 162
xor_eq Avatar answered Sep 21 '22 10:09

xor_eq


Well, no, I don't believe there is a way to avoid overwriting existing content with a single, standard Java IO API call.

If the files are not too large, just read the entire file into an ArrayList (an entry per line) and either rewrite entries or insert new entries for new lines.

Then overwrite the existing file with new content, or move the existing file to a backup and write a new file.

Depending on how sophisticated the edits need to be, your data structure may need to change.

Another method would be to read characters from the existing file while writing to the edited file and edit the stream as it is read.

like image 36
Ken Gentle Avatar answered Sep 18 '22 10:09

Ken Gentle