I have encountered a problem that confused me for some time.
In Linux, I logged in as a root user. Then I wrote a Java program that used RandomAccessFile
to write data to disk first, then I deleted the file.
But the program continued to run without throwing any exception! But I couldn't find the written data even in the Linux PageCache! The following is the code snippet:
RandomAccessFile file = new RandomAccessFile("/opt/temp.log", "rw");
byte[] content = "Hello!!!\n".getBytes();
for (int i = 0; i < 2; i++) {
file.write(content, 0, content.length);
Thread.sleep(3000);//just for lefting time to delete the "temp.log" file!
}
But if I change the file.write()
method to file.writeBytes()
, it regenerated the new file and wrote the data to the file. The following is the code snippet:
RandomAccessFile file = new RandomAccessFile("/opt/temp.log", "rw");
for (int i = 0; i < 2; i++) {
file.writeBytes("Hello!!!\n");
Thread.sleep(3000);//just for lefting time to delete the "temp.log" file!
}
What happened? Is there a bug in RandomAccessFile
?