Listing All Drivers and Its Information using Java

This program all about Listing All Drivers and Its Information using Java. Here I have attached, Simple program about Listing all drives with type, total files, total space and free space in Java. The program was tested and output is shared in the same post.

In this example, I have used couple of methods from java.io.File and javax.swing.filechooser.FileSystemView classes. Find the below program to get the idea.

FileSystemOverview.java

package com.javatraineronline;
import java.io.File;
import javax.swing.filechooser.FileSystemView;
public class FileSystemOverview {
// Defining FileSystemView to get the File Type Description
private FileSystemView fileSystemView = FileSystemView.getFileSystemView();
private String getDriverTypeDesc(File file) {
return fileSystemView.getSystemTypeDescription(file);
}
// Method used to get the Free space and Total space 
private String spaceCalculation(File file, String type) {
if ("T".equalsIgnoreCase(type)) {
long space = file.getTotalSpace();
return ((space / 1024) / 1024) + " MB" + " or "
+ (((space / 1024) / 1024) / 1024) + " GB";
} else if ("F".equalsIgnoreCase(type)) {
long space = file.getFreeSpace();
return ((space / 1024) / 1024) + " MB" + " or "
+ (((space / 1024) / 1024) / 1024) + " GB";
}
return "";
}
// File and Directory Count
private int getFileAndDiretoryCount(File file) {
return file.listFiles().length;
}
// Get Directory Count
private int getDirectoryCount(File file) {
int sum = 0;
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
++sum;
}
}
}
return sum;
}
// Get File Count
private int getFileCount(File file) {
int sum = 0;
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isFile()) {
++sum;
}
}
}
return sum;
}
public void getDriverInformation() {
File[] fileArray = File.listRoots(); // Getting list of Drivers form Computer
if (fileArray != null && fileArray.length > 0) {
System.out.println("----------------------------------------");
for (File file : fileArray) {
System.out.println("Driver Name : " + file);
System.out.println("Driver Type : " + getDriverTypeDesc(file));
System.out.println("Driver Total Space : "
+ spaceCalculation(file, "T"));
System.out.println("Available Free Space : "
+ spaceCalculation(file, "F"));
System.out.println("Available Directory Count : "+getDirectoryCount(file));
System.out.println("Available File Count : "+getFileCount(file));
System.out
.println("Total Files & Directory Count : "
+ getFileAndDiretoryCount(file));
System.out.println("----------------------------------------");
}
}
}
public static void main(String[] args) {
// Defining the Object
FileSystemOverview fso = new FileSystemOverview();
fso.getDriverInformation();
}
}

Output

—————————————-
Driver Name : C:\
Driver Type : Local Disk
Driver Total Space : 32365 MB or 31 GB
Available Free Space : 198 MB or 0 GB
Available Directory Count : 25
Available File Count : 3
Total Files & Directory Count : 28
—————————————-
Driver Name : D:\
Driver Type : Local Disk
Driver Total Space : 10236 MB or 9 GB
Available Free Space : 393 MB or 0 GB
Available Directory Count : 21
Available File Count : 1
Total Files & Directory Count : 22
—————————————-

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *