DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Getting File System Details in Java

Getting File System Details in Java

Daniel Pietraru user avatar by
Daniel Pietraru
·
May. 04, 08 · Interview
Like (0)
Save
Tweet
Share
46.43K Views

Join the DZone community and get the full member experience.

Join For Free
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/

 

operating system File system Java (programming language)

Published at DZone with permission of Daniel Pietraru. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Validate Three Common Document Types in Python
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Using the PostgreSQL Pager With MariaDB Xpand
  • Core Machine Learning Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: