开发者

Writing in the beginning of a text file Java

I need to write something into a text file's beginning. I have a text file with content and i want write something before this conte开发者_如何转开发nt. Say i have;

Good afternoon sir,how are you today?

I'm fine,how are you?

Thanks for asking,I'm great

After modifying,I want it to be like this:

Page 1-Scene 59

25.05.2011

Good afternoon sir,how are you today?

I'm fine,how are you?

Thanks for asking,I'm great

Just made up the content :) How can i modify a text file like this way?


You can't really modify it that way - file systems don't generally let you insert data in arbitrary locations - but you can:

  • Create a new file
  • Write the prefix to it
  • Copy the data from the old file to the new file
  • Move the old file to a backup location
  • Move the new file to the old file's location
  • Optionally delete the old backup file


Just in case it will be useful for someone here is full source code of method to prepend lines to a file using Apache Commons IO library. The code does not read whole file into memory, so will work on files of any size.

public static void prependPrefix(File input, String prefix) throws IOException {
    LineIterator li = FileUtils.lineIterator(input);
    File tempFile = File.createTempFile("prependPrefix", ".tmp");
    BufferedWriter w = new BufferedWriter(new FileWriter(tempFile));
    try {
        w.write(prefix);
        while (li.hasNext()) {
            w.write(li.next());
            w.write("\n");
        }
    } finally {
        IOUtils.closeQuietly(w);
        LineIterator.closeQuietly(li);
    }
    FileUtils.deleteQuietly(input);
    FileUtils.moveFile(tempFile, input);
}


I think what you want is random access. Check out the related java tutorial. However, I don't believe you can just insert data at an arbitrary point in the file; If I recall correctly, you'd only overwrite the data. If you wanted to insert, you'd have to have your code

  1. copy a block,
  2. overwrite with your new stuff,
  3. copy the next block,
  4. overwrite with the previously copied block,
  5. return to 3 until no more blocks


As @atk suggested, java.nio.channels.SeekableByteChannel is a good interface. But it is available from 1.7 only.

Update : If you have no issue using FileUtils then use

String fileString = FileUtils.readFileToString(file);


This isn't a direct answer to the question, but often files are accessed via InputStreams. If this is your use case, then you can chain input streams via SequenceInputStream to achieve the same result. E.g.

InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream("my line\n".getBytes()), new FileInputStream(new File("myfile.txt")));


I will leave it here just in case anyone need

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try (FileInputStream fileInputStream1 = new FileInputStream(fileName1);
         FileInputStream fileInputStream2 = new FileInputStream(fileName2)) {

        while (fileInputStream2.available() > 0) {
            byteArrayOutputStream.write(fileInputStream2.read());
        }
        while (fileInputStream1.available() > 0) {
            byteArrayOutputStream.write(fileInputStream1.read());
        }
    }
    try (FileOutputStream fileOutputStream = new FileOutputStream(fileName1)) {
        byteArrayOutputStream.writeTo(fileOutputStream);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜