2

我尝试了以下程序

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);
        }
    }
}

但在我的系统中软盘驱动器未连接,我收到如下消息

“驱动器尚未准备好使用;它的门可能打开,请检查驱动器A:并确保磁盘已插入并且驱动器门已关闭”然后显示三个选项取消,再试一次,当我尝试继续时继续,它有效,但我怎么能避免那个味精

4

4 回答 4

2

你想做什么?

我的建议是使用FileSystemView

它使用了这样的东西:

FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (File f: roots) {
    System.out.println(fsv.getSystemDisplayName(f);
}
于 2009-08-19T07:39:55.100 回答
2
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());
    }
}

}

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

于 2009-08-19T07:41:30.227 回答
2

说到Windows,这个WindowsAltFileSystemView提出了一个基于FileSystemView的替代方案

这个类是必要的,因为 Windows NT 上有一个烦人的错误,在该错误中,JFileChooser使用默认值实例化 a每次FileSystemView都会导致 " " 错误。 我从 1.3 SDK 中获取了 Windows impl 并对其进行了修改,以便 * 不用于获取 fileSystem 根目录。drive A: not ready
FileSystemViewjava.io.File.listRoots()

java.io.File.listRoots()这样做会导致操作系统即使在没有磁盘的情况下也SecurityManager.checkRead()尝试访问驱动器A:abort, retry, ignore,从而导致每次我们实例化 a 时都会出现烦人的“ ”弹出消息JFileChooser

所以在这里,想法是扩展FileSystemView,将getRoots()方法替换为:

   /**
     * 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;
    }
于 2009-08-19T07:43:19.477 回答
0

你可以用这个;

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());
         }
    }
}
于 2019-12-08T10:43:44.573 回答