views:

7701

answers:

6

Hello,

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.

+1  A: 

I believe the only way to insert text into an existing text file is to read the original file and write the content in a temporary file with the new text inserted. Then erase the original file and rename the temporary file to the original name.

This example is focused on inserted a single line into an existing file, but still maybe of use to you.

bcash
+1  A: 

I don't know if there's a handy way to do it straight otherwise than

  • read the beginning of the file and write it to target
  • write your new text to target
  • read the rest of the file and write it to target.

About the target : You can construct the new contents of the file in memory and then overwrite the old content of the file if the files handled aren't so big. Or you can write the result to a temporary file.

The thing would probably be easiest to do with streams, RandomAccessFile doesn't seem to be meant for inserting in the middle (afaik). Check the tutorial if you need.

Touko
+4  A: 

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.

Ken Gentle
+1  A: 

If Java has a way to memory map files, then what you can do is extend the file to its new length, map the file, memmove all the bytes down to the end to make a hole and write the new data into the hole.

This works in C. Never tried it in Java.

Another way I just thought of to do the same but with random file access.

  • Seek to the end - 1 MB
  • Read 1 MB
  • Write that to original position + gap size.
  • Repeat for each previous 1 MB working toward the beginning of the file.
  • Stop when you reach the desired gap position.

Use a larger buffer size for faster performance.

Zan Lynx
A: 

If it is a text file,,,,Read the existing file in StringBuffer and append the new content in the same StringBuffer now u can write the SrtingBuffer on file. so now the file contains both the existing and new text.