when getting drive information using java

北城余情 提交于 2019-12-01 12:04:02

What are you trying to do?

My recommendation would be to use FileSystemView.

It's used something like this:

FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (File f: roots) {
    System.out.println(fsv.getSystemDisplayName(f);
}
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 byFileSystemView.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());
    }
}

}

reference : http://littletutorials.com/2008/03/10/getting-file-system-details-in-java/

When it comes to Windows, this class WindowsAltFileSystemView proposes an alternative based on FileSystemView

This class is necessary due to an annoying bug on Windows NT where instantiating a JFileChooser with the default FileSystemView will cause a "drive A: not ready" error every time.
I grabbed the Windows FileSystemView impl from the 1.3 SDK and modified it so * as to not use java.io.File.listRoots() to get fileSystem roots.

java.io.File.listRoots() does a SecurityManager.checkRead() which causes the OS to try to access drive A: even when there is no disk, causing an annoying "abort, retry, ignore" popup message every time we instantiate a JFileChooser!

So here, the idea is to extends FileSystemView, replacing the getRoots() method with:

   /**
     * Returns all root partitians on this system. On Windows, this
     * will be the A: through Z: drives.
     */
    public File[] getRoots() {
        Vector rootsVector = new Vector();

        // Create the A: drive whether it is mounted or not
        FileSystemRoot floppy = new FileSystemRoot("A" + ":" + "\\");
        rootsVector.addElement(floppy);

        // Run through all possible mount points and check
        // for their existance.
        for (char c = 'C'; c <= 'Z'; c++) {
            char device[] = {c, ':', '\\'};
            String deviceName = new String(device);
            File deviceFile = new FileSystemRoot(deviceName);
            if (deviceFile != null && deviceFile.exists()) {
                rootsVector.addElement(deviceFile);
            }
        }
        File[] roots = new File[rootsVector.size()];
        rootsVector.copyInto(roots);
        return roots;
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!