Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write to the end of a 5GB file in Java?

Tags:

java

file-io

Can I write to the end of a 5GB file in Java? This question came up in my office and no one is sure what the answer is.

like image 807
David Locke Avatar asked Dec 03 '08 21:12

David Locke


People also ask

How read and write large files in Java?

Using BufferedReader and Java Streams To do that, we will use BufferedReader, which provides a Stream of strings read from the file. Next is an example of using Java Stream provided by BufferedReader to process a very very large file (10GB). Now, we will test the method that uses BufferedReader to read a 10GB file.

Is BufferedWriter faster?

Write text files faster with BufferedWriter BufferedWriter adds another 8 KB buffer for characters, which are then encoded in one go when the buffer is written (instead of character by character). This second buffer reduces the writing time for 100,000,000 characters to approximately 370 ms.


2 Answers

This should be possible fairly easily using a RandomAccessFile. Something like the following should work:

String filename;

RandomAccessFile myFile = new RandomAccessFile(filename, "rw");

// Set write pointer to the end of the file
myFile.seek(myFile.length());

// Write to end of file here
like image 181
Greg Case Avatar answered Sep 21 '22 08:09

Greg Case


Yes. Take a look at this link RandomAccessFile

http://java.sun.com/javase/6/docs/api/java/io/RandomAccessFile.html#seek(long)

That is , you open the file, and then set the position to the end of the file. And start writing from there.

Tell us how it went.

like image 31
OscarRyz Avatar answered Sep 25 '22 08:09

OscarRyz