I've looked at java.nio.file.attribute.Attributes
and java.nio.file.FileStore
, but couldn't find a way to discover the block-size of a disk-file.
Asked
Active
Viewed 1,693 times
4

ThinkingStiff
- 64,767
- 30
- 146
- 239

Steve Emmerson
- 7,702
- 5
- 33
- 59
-
1Isn't block size intrinsically non-portable? What if the file store doesn't use blocks (sure unlikely, but possible)? – Mark Peters Oct 08 '10 at 15:54
-
2@Mark The vast majority of file-stores are implemented via spinning disks and, consequently, use blocks. I can handle the case where the block size is one byte. What I want is a portable way to determine that size. – Steve Emmerson Oct 10 '10 at 18:49
2 Answers
0
Here is an article on buffer size vs I/O throughput. Synopsis: nio handles all of that for you. This SO post concludes you should use a power of 2.

Community
- 1
- 1

Devon_C_Miller
- 16,248
- 3
- 45
- 71
-
1Devon, I want a way to determine the block size programmatically that doesn't rely on conducting an experiment and interpreting the results. – Steve Emmerson Oct 10 '10 at 19:02
-
1I understand that. However, there is no mechanism in Java that exposes the fielsystem's preferred IO size. Given that, the best fall back position is to use a likely multiple and trust the io/nio subsystem to do the right thing. Generally speaking, filesystems use I/O sizes of 4K, 8K, or 16K. So, using one of those sizes will generally be sufficient. – Devon_C_Miller Oct 11 '10 at 13:41
0
Java 7 does not have the requested information, and the users question is related to Java 7.
But in Java 10, there was a new method introduced which provides the requested information.
The getBlockSize() method of a FileStore class is used to return the number of bytes per block in this file store Object. Every File storage is organized into discrete sequences of bytes called blocks and a block is the smallest storage unit of a file store. Every read and write operation is performed on multiple blocks. This method is helpful in getting the size of Block.

Valamburi M
- 692
- 4
- 17

user14421942
- 1
- 1