1

I just stopped by a question about this subject.

Say when I do like this. (Note that I used "rw" not "rws" or "rwd").

try(RandomAccessFile raw = new RandomAccessFile(file, "rw")) {
    // write some
}

Does RandomAccessFile#close() do as if I explicitly do getFD().sync()?

try(RandomAccessFile raw = new RandomAccessFile(file, "rw")) {
    // write some
    raw.getFD().sync();
}

I searched RandomAccessFile.java and the source just do

public void close() throws IOException {
    // doin' here, things look like irrelevant
    close0();
}

private native void close0() throws IOException;
Community
  • 1
  • 1
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • Yes and no. Eventually the data will get to disk. But no, there is absolutely no guarantee of when that happens. Not when close returns, not even when the JVM exits. But probably almost certainly most of the time very soon after close is called. – President James K. Polk Jan 23 '16 at 02:25
  • @JamesKPolk Interesting. Is that fact should lead me to call `getFD().sync()` explicitly? – Jin Kwon Jan 23 '16 at 02:30

0 Answers0