Hi, I've tried the code below on both Windows (64bit) and Linux(32bit).
I was sure that without BufferedOutputStream the code is bound to throw OutOfMemoryException yet it didn't.
Why is that? Who is doing the {caching / buffer / steaming} to disk there?
Can you please describe, if relevant to the answer, the full flow (Java API -> system call) ?
Does this code uses NIO?
/Me confused.
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteHugeFileToDisk {
private static int BYTE = 1;
private static int KILBYTE = BYTE * 1024;
private static int MEGABYTE = KILBYTE * 1024;
private static int GIGABYTE = MEGABYTE * 1024;
private static long TERABYTE = GIGABYTE * 1024L;
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(args[0]);
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
byte[] buffer = new byte[MEGABYTE];
for(int i = 0; i < buffer.length; i++) {
buffer[i] = (byte)i;
}
for(long l = 0; l < 4000; l++) {
dataOutputStream.write(buffer);
;
}
}
}
I've ran this code with Java 6. Using the following invocations:
Windows:
java WriteHugeFileToDisk %TEMP%\hi.txt
Linux:
java WriteHugeFileToDisk /mnt/hi.info
Please note: The code creates 4GB file full of just for the test.