Listing computer drives and its type using Java


In this tutorial, you will learn how to list all computer drives and its type using Java programme. In Java, you use the File.listRoots() to list all the available drives of your computer. listRoots() is a static method in File class which return the array of available filesystem roots.

File[] computerDrives = File.listRoots();

for (File drive : computerDrives) {
	System.out.println("Drive Name : " + drive);
}

To get the type of drive, you use the getSystemTypeDescription(File f) method of FileSystemView class.

File[] computerDrives = File.listRoots();
FileSystemView fsv = FileSystemView.getFileSystemView();

for (File drive : computerDrives) {
	System.out.println("Drive Name : " + drive + " Drive Type : " + fsv.getSystemTypeDescription(drive));
}

Let’s see the complete example.

DrivesAndTypeDemo.java
package org.websparrow.file;

import java.io.File;

import javax.swing.filechooser.FileSystemView;

public class DrivesAndTypeDemo {

	public static void main(String[] args) {

		File[] computerDrives = File.listRoots();
		FileSystemView fsv = FileSystemView.getFileSystemView();

		if (computerDrives != null && computerDrives.length > 0) {

			for (File drive : computerDrives) {
				System.out.println("Drive Name : " + drive + " Drive Type : " + fsv.getSystemTypeDescription(drive));
			}
		}
	}

}

Run the above programme and you get the output as given below.

Drive Name : C:\ Drive Type : Local Disk
Drive Name : D:\ Drive Type : Local Disk
Drive Name : E:\ Drive Type : Local Disk
Drive Name : F:\ Drive Type : Local Disk
Drive Name : G:\ Drive Type : CD Drive

Note: Tested with Windows operating system.


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.