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

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

+6
java file-io
source share
5 answers

This should be possible quite easily using 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 
+12
source share

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.

+6
source share

In fact, it depends on the underlying file system and how the JVM on this platform implements File Stream. Because if the file is larger than 5 GB, you cannot, with a 32-bit operating system, open the entire file and simply write to it, because of the 4.3-bit limit (32 ^ 2).

So, the answer will be soon: Yes, it is possible if Java processes the file correctly, and the File System is good :)

+2
source share

If you just want to add to the file, check

 FileWriter(File file, boolean append) 

in the class FileWriter .

Sorry, I don’t have a 5 GB file that I can test with. :)

+1
source share

5GB? I wonder if the OS is a more serious problem, but it is doubtful.

In theory, you can simply open the file in add mode.

 OutputStream in = new java.io.FileOutputStream(fileName, true); 

and write before filling in the file system.

See "Bill of the Lizards" for char data.

0
source share

All Articles