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.
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.
I don't think there is a java function to do that. However there is a system dependent way to retrieve that information. Have a look at this answer. You'll need to use Runtime.exec() and parse the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With