How To Check Type Of Drive (Hard Disk / Removable Disk) In Java

This example shows how to check if for a hard disk or removable disk in the system. FileSystemView.isFloppyDrive() returns if the disk is removable. If you look at this example, it prints some of the details about the drive like read, write, etc.

DrivesExample.java

package javabeat.net.io;

import java.io.File;

import javax.swing.filechooser.FileSystemView;

/**
 * Find Hard Disk or Removable Disk Example
 *
 * @author Krishna
 *
 */
public class DrivesExample {

	public static void main(String[] args) {
	      FileSystemView fileSystemView = FileSystemView.getFileSystemView();
	      System.out.println("Home directory: " + fileSystemView.getHomeDirectory());

	      File[] fileRoots = File.listRoots();
	      for (int i = 0; i < fileRoots.length; i++)
	      {
	    	System.out.println();
	        System.out.println("System Drive: " + fileRoots[i]);
	        System.out.println("Drive Display name: " + fileSystemView.getSystemDisplayName(fileRoots[i]));
	        System.out.println("Is drive: " + fileSystemView.isDrive(fileRoots[i]));
	        System.out.println("Is floppy: " + fileSystemView.isFloppyDrive(fileRoots[i]));
	        System.out.println("Readable: " + fileRoots[i].canRead());
	        System.out.println("Writable: " + fileRoots[i].canWrite());
	        System.out.println();
	      }
	   }

}

Output…

Home directory: C:\Users\krishna\Desktop

Drive: C:\
Display name: OSDisk (C:)
Is drive: true
Is floppy: false
Readable: true
Writable: true

Drive: D:\
Display name: DATA (D:)
Is drive: true
Is floppy: false
Readable: true
Writable: true

Drive: Q:\
Display name:
Is drive: true
Is floppy: false
Readable: false
Writable: false

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*