when getting drive information using java

2020-03-26 11:26发布

i tried the following program

import java.io.*;
class dr
{     
  public static void main(String args[])
    {
      try{
           File[] roots = File.listRoots();     
           for (int index = 0; index < roots.length; index++)    
           { //Print out each drive/partition          
            System.out.println(roots[index].toString());    
           }
         }
      catch(Exception e)
        { 
         System.out.println("error   " +e);
        }
    }
}

but in my system floppy drive is not connected and i am getting a message like the following

" The drive is not ready for use;its door may be open,Please check drive A: and make sure that disk is inserted and that the drive door is closed" then three options are shown cancel,try again,continue when i try continue,it works but how i could avoid that msg

标签: java drive
4条回答
The star\"
2楼-- · 2020-03-26 12:04
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/

查看更多
【Aperson】
3楼-- · 2020-03-26 12:07

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;
    }
查看更多
虎瘦雄心在
4楼-- · 2020-03-26 12:10

you can use this;

import java.io.File;
class dr
{     
  public static void main(String args[])
    {
         File[] roots=File.listRoots();
         for(File root:roots)
         {
             System.out.println(root.getName());
         }
    }
}
查看更多
可以哭但决不认输i
5楼-- · 2020-03-26 12:29

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);
}
查看更多
登录 后发表回答