Get Drive Size in Java 5

I want to get the size of a drive (or UNC path pointing to a partition would be nice, but not required), as well as free space for said drive (or UNC path). This doesn't need to work cross platform; only in Windows.

I know it's easy to do in Java 6, but that's not an option; I'm stuck with Java 5.

I can get the free space available by doing:

cmd.exe /c Z:\ /-c

or

cmd.exe /c \\server\share /-c

and just parsing out the resulting bytes free. However I can't seem to find a way to get the total drive size.

Any suggestions?


Asked by: Daryl700 | Posted: 21-01-2022






Answer 1

One way to do it would be to use fsutil on the command line. It returns something like this:

D:\>fsutil fsinfo ntfsinfo c:
NTFS Volume Serial Number :       0xd49cf9cf9cf9ac5c
Version :                         3.1
Number Sectors :                  0x0000000004a813ff
Total Clusters :                  0x000000000095027f
Free Clusters  :                  0x00000000002392f5
Total Reserved :                  0x0000000000000490
Bytes Per Sector  :               512
Bytes Per Cluster :               4096
Bytes Per FileRecord Segment    : 1024
Clusters Per FileRecord Segment : 0
Mft Valid Data Length :           0x000000000e70c000
Mft Start Lcn  :                  0x00000000000c0000
Mft2 Start Lcn :                  0x0000000000000010
Mft Zone Start :                  0x0000000000624ea0
Mft Zone End   :                  0x0000000000643da0

Multipy your number of sectors times the bytes per sector to get your size.

Answered by: Sophia237 | Posted: 22-02-2022



Answer 2

You could do this pretty easily using a JNI call if you are comfortable with that...

If you want a pre-packaged library that you can use with JDK1.5, take a look at the Apache FileSystemUtils

This just wraps the system call that you describe, but at least it's a standard library that you can use until you are able to use 1.6.

Answered by: Dainton305 | Posted: 22-02-2022



Answer 3

You could use the SIGAR library, which gives you native access on many platforms.

Answered by: Vanessa252 | Posted: 22-02-2022



Similar questions

when getting drive information using java

i tried the following program import java.io.*; class dr { public static void main(String args[]) { try{ File[] roots = File.listRoots(); for (int index = 0; index < roots.length; index++) { //Print out each drive/partition System.out.println(roots[index].toString()); } } catch(Exception e) ...


java - Drive Api Phone

I'm trying to create a java program that will create and upload excel files into a google drive. After uploading I need it to give permissions. I have done all thatbut the problem lies in trying to convert the excel files into a google files in order that people who use the sheets are able to update them with their phones. I have not been able to find any help on how to do this on the google api. Any help will be appreciat...


java - Index my Hard drive into solr

I want to index my system each files and folders information into solr. Is there any handler in solr to do it? I can index any file using data import handler using below command curl "http://localhost:8983/solr/update/extract?ext.idx.attr=true\&ext.def.fl=text&commit=true" -F "myfile=@tmp1.txt" But i want to index my whole system into solr.


java - Drive REST API use wifi only

Previously, Google Drive API for Android was allowing to make a setting, that the data transfer was only done, when device is connected to the internet via WIFI: https://developers.google.com/android/reference/com/google/android/gms/drive/TransferPreference...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top