Daniel has posted 4 posts at DZone. View Full User Profile

Getting File System Details in Java

05.04.2008
| 31469 views |
  • submit to reddit
Due to a number of differences between various platforms it is very difficult to present system specific information in a consistent manner. When getting closer to system specific details, like file system information, a Java programmer has to become aware of the operating system hosting his program in order to make sense of the information returned by some of the Java APIs. Getting detailed information about a file system in Java is pretty difficult without using system specific code like JNI or at least accessing various specific operating system resources like configuration files.
Java offers two ways of getting some file system information through java.io.File and javax.swing.filechooser.FileSystemView classes. On some systems like Linux the information obtained from these two APIs is identical, while on others like Windows is quite different.

The sample code shows how to get file system information and the output differences on Windows and Solaris:

package com.littletutorials.fs;

import java.io.*;
import javax.swing.filechooser.*;

public class DriveTypeInfo
{
public static void main(String[] args)
{
System.out.println("File system roots returned by FileSystemView.getFileSystemView():");
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (int i = 0; i < roots.length; i++)
{
System.out.println("Root: " + roots[i]);
}

System.out.println("Home directory: " + fsv.getHomeDirectory());

System.out.println("File system roots returned by File.listRoots():");
File[] f = File.listRoots();
for (int i = 0; i < f.length; i++)
{
System.out.println("Drive: " + f[i]);
System.out.println("Display name: " + fsv.getSystemDisplayName(f[i]));
System.out.println("Is drive: " + fsv.isDrive(f[i]));
System.out.println("Is floppy: " + fsv.isFloppyDrive(f[i]));
System.out.println("Readable: " + f[i].canRead());
System.out.println("Writable: " + f[i].canWrite());
System.out.println("Total space: " + f[i].getTotalSpace());
System.out.println("Usable space: " + f[i].getUsableSpace());
}
}
}

Running this code on Windows will produce this kind of output:

File system roots returned by FileSystemView.getFileSystemView():
Root: C:\Documents and Settings\daniel\Desktop

Home directory: C:\Documents and Settings\daniel\Desktop

File system roots returned by File.listRoots():
Drive: A:\
Display name:
Is drive: true
Is floppy: true
Readable: false
Writable: false
Total space: 0
Usable space: 0
Drive: C:\
Display name: Data (C:)
Is drive: true
Is floppy: false
Readable: true
Writable: true
Total space: 79990812672
Usable space: 39353810944
Drive: D:\
Display name: Backup (D:)
Is drive: true
Is floppy: false
Readable: true
Writable: false
Total space: 717684736
Usable space: 0
Drive: H:\
Display name: daniel on 'File Server (Filesvr)' (H:)
Is drive: true
Is floppy: false
Readable: true
Writable: true
Total space: 1310720000
Usable space: 801497088

Running this same code on Solaris displays:

File system roots returned by FileSystemView.getFileSystemView():
Root: /

Home directory: /home/daniel

File system roots returned by File.listRoots():
Drive: /
Display name: /
Is drive: false
Is floppy: false
Readable: true
Writable: false
Total space: 1310720000
Usable space: 801497088

As you can see making sense of the information in a useful way requires to know in advance the kind of operating system your program is running on.
Unfortunately here is where the abstraction breaks and the differences leak into the code. For example based on the information above it is almost impossible to detect a CD-ROM drive with 100% certainty without resorting to JNI code or some similar solution.

This article was originally posted at http://littletutorials.com/2008/03/10/getting-file-system-details-in-java/

 

4
Your rating: None Average: 4 (2 votes)
Published at DZone with permission of its author, Daniel Pietraru.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

David Voo replied on Sun, 2008/05/04 - 8:41pm

Suggest to add OS detection and uses designated OS specific APis

Daniel Pietraru replied on Tue, 2008/05/06 - 1:06pm

OS detection is a must do in this kind of situations. What do you mean by "designated OS specific APis"?

Harsha vardhan replied on Thu, 2009/07/30 - 4:12am

how to display the available drive letters which are not present in the system using java?

 

for eg: I have A:\, C:\, D:\, E:\ drives in the system and X:\, Y:\, Z:\ drives are mapped. But i need to display the drive letters other than these letters in a combo... please suggest me how to proceed?

Jagadeesh Kavani replied on Mon, 2011/07/25 - 5:27am

It was fine.

But it simply telling whether the given drive is Floppy drive or not by using the method "isFloppyDrive(file[i])"

But in Windows we have many FileSystems like 

1. NTFS,

2. FTS,

3. CDFS(cd FileSystem when u insert CD in CD drive)

note: to check this CDFS insert cd and right click on CD Drive goto properties u will find there like FileSystem:CDFS

4. USBFS(USB FileSystem when you insert PenDrive)

So, is it possible to know the FileSystem of All drives in a System

For Ex:-

If I have 4 HardDisk Drives(C,D,E,F)(C and D with NTFS, E and F with FTS)

2 Pen Drives (H,I)

1 CD drive(G: CD inserted)

I wish to have output like

----------------------------

 C drive has NTFS

 D drive has NTFS

 E drive has FTS

 F drive has FTS

 G drive has CDFS

 H drive has USBFS

 I drive has USBFS

-----------------------------

please send your suggestions

 

Thanks in advance

Jdsh

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.