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:

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.



Attachments:
Download this file (DrivesListingExample.java)DrivesListingExample.java[Example program]0.7 kB

Add comment

   


Comments 

#9BABLU KUMAR BAIRWA2020-07-30 09:13
I LIKE IT
COMPUTER IN WRIRE WORKING
Quote
#8Kurt2019-10-13 10:00
I was searching for a way to find out if a drive was a USB drive. Got it now. Thanks man!
Quote
#7JOhn2017-12-22 01:03
If the CMD prompt is open in administrator privilege. It does not show network drives.
Quote
#6kevin2015-04-20 11:57
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.
Quote
#5Nam2014-06-27 21:23
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.
Quote