Get system block size in Java

file-io, java, operating-system, optimization, system

Solution

The 'fastest most optimal way possible' is surely to write using the biggest buffer you can afford; to make sure its size is a power of two (a megabyte comes to mind); and to make sure that the writes themselves are buffer-aligned:

new BufferedOutputStream(new FileOutputStream(file), 1024*1024);

As long as you are over the system block size, which you will be at this size, and remain aligned with it, which is guaranteed by the `BufferedOutputStream`, this is about as optimal as it gets.

You should also look into `FileChannel.transferTo()`, noting that you must call it in a loop, and that the actual implementations so far don't appear to use any low-level operating system primitives (contrary to the advertising), just the same kind of loop you could write yourself.

Problem

I'm trying to write the fastest, most optimal file saving method possible. Is there any way to get the system block size in java? Something like `System.getProperty("block.size")` or something.

Original source