The above code snippet would produce the following output (e.g. on Windows platform):
C:\
D:\
E:\
F:\
The output may vary, depending on system configuration and platform, e.g. there would be only one root partition “\” on a Linux/Unix system.
Getting type description of a drive:
To know which type (hard disk, CD-ROM, flash drive, etc) of a drive, we can use the method getSystemTypeDescription(File) provided by the FileSystemView class in the javax.swing.filechooser package. For example:
Since Java 1.6, we can know the total space and free space of a drive partition by using the getTotalSpace() and getFreeSpace() methods, respectively. These methods return the spaces in bytes. For example:
On Windows:
File aDrive = new File("C:");
long freeSpace = aDrive.getFreeSpace();
long totalSpace = aDrive.getTotalSpace();
On Linux/Unix:
File aDrive = new File("\\");
long freeSpace = aDrive.getFreeSpace();
long totalSpace = aDrive.getTotalSpace();
Java Listing Drivers Example Program:
The following program lists all available drive partitions with their drive letters, type descriptions, free spaces and total spaces:
Drive Letter: C:\
Type: Local Disk
Total space: 73402363904
Free space: 11994337280
Drive Letter: D:\
Type: Local Disk
Total space: 106151542272
Free space: 84617833472
Drive Letter: E:\
Type: Local Disk
Total space: 106232282624
Free space: 70415875072
Drive Letter: F:\
Type: CD Drive
Total space: 0
Free space: 0
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
am working on a system call program in linux and i really need some help in working with the usb function calls, can someone post on who to get the attributes of a usb drive ,rename the drive ,eject it and remount it.
hi Reginald, You have nice example and good questions. Looking at the Javadoc of the File class, I think they clarified the differences between the getFreeSpace() and getUsableSpace() methods. Here's the Javadoc says:
- getFreeSpace(): Returns the number of unallocated bytes...This method makes no guarantee that write operations to this file system will succeed.
- getUsableSpace(): Returns the number of bytes available to this virtual machine ...., this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace().
That means usable space is the capacity you can write to the disk. And you may not write data to the free space if it is not usable.
Comments
COMPUTER IN WRIRE WORKING
,rename the drive ,eject it and remount it.
You have nice example and good questions. Looking at the Javadoc of the File class, I think they clarified the differences between the getFreeSpace() and getUsableSpace() methods. Here's the Javadoc says:
- getFreeSpace(): Returns the number of unallocated bytes...This method makes no guarantee that write operations to this file system will succeed.
- getUsableSpace(): Returns the number of bytes available to this virtual machine ...., this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace().
That means usable space is the capacity you can write to the disk. And you may not write data to the free space if it is not usable.