- 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:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.