Java File IO - Listing all drives with type, total space and free space
- Details
- Written by Nam Ha Minh
- Last Updated on 29 July 2019   |   Print Email
In Java File I/O programming, to list all available drive partitions on computer, we can use the following code snippet:
File[] drives = File.listRoots(); if (drives != null && drives.length > 0) { for (File aDrive : drives) { System.out.println(aDrive); } }
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:
FileSystemView fsv = FileSystemView.getFileSystemView(); String driveType = fsv.getSystemTypeDescription(aDrive);
Getting total space and free space of a drive:
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:
package net.codejava.io; import java.io.File; import javax.swing.filechooser.FileSystemView; /** * * @author www.codejava.net * */ public class DrivesListingExample { public static void main(String[] args) { FileSystemView fsv = FileSystemView.getFileSystemView(); File[] drives = File.listRoots(); if (drives != null && drives.length > 0) { for (File aDrive : drives) { System.out.println("Drive Letter: " + aDrive); System.out.println("\tType: " + fsv.getSystemTypeDescription(aDrive)); System.out.println("\tTotal space: " + aDrive.getTotalSpace()); System.out.println("\tFree space: " + aDrive.getFreeSpace()); System.out.println(); } } } }
Sample output on Windows:
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
Sample output on Unix/Linux:
Drive Letter: / Type: null Total space: 30829133824 Free space: 22259314688
Other Java File IO Tutorials:
- How to Read and Write Text File in Java
- How to Read and Write Binary Files in Java
- Java IO - Common File and Directory Operations Examples
- Java Serialization Basic Example
- Understanding Java Externalization with Examples
- How to execute Operating System Commands in Java
- 3 ways for reading user's input from console in Java
- File change notification example with Watch Service API
- Java Scanner Tutorial and Code Examples
- How to compress files in ZIP format in Java
- How to extract ZIP file in Java
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.