Really force file sync/flush in Java

Actually, in C you want to just call fsync() on the one file descriptor, not sync() (or the “sync” command) which signals the kernel to flush all buffers to disk system-wide.

If you strace (getting Linux-specific here) the JVM you should be able to observe an fsync() or fdatasync() system call being made on your output file. That would be what I’d expect the getFD().sync() call to do. I assume c.force(true) simply flags to NIO that fsync() should be called after each write. It might simply be that the JVM you’re using doesn’t actually implement the sync() call?

I’m not sure why you weren’t seeing any difference when calling “sync” as a command: but obviously, after the first sync invocation, subsequent ones are usually quite a lot faster. Again, I’d be inclined to break out strace (truss on Solaris) as a “what’s actually happening here?” tool.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)