【问题标题】:when getting drive information using java使用 java 获取驱动器信息时
【发布时间】:2019-12-08 18:43:44
【问题描述】:

我尝试了以下程序

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:并确保磁盘已插入并且驱动器门已关闭" 然后显示三个选项取消,再试一次,继续 当我尝试继续时,它可以工作 但我怎么能避免那个味精

【问题讨论】:

    标签: java drive


    【解决方案1】:

    你想做什么?

    我的建议是使用FileSystemView。

    它是这样使用的:

    FileSystemView fsv = FileSystemView.getFileSystemView();
    File[] roots = fsv.getRoots();
    for (File f: roots) {
        System.out.println(fsv.getSystemDisplayName(f);
    }
    

    【讨论】:

      【解决方案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/

      【讨论】:

        【解决方案3】:

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

        这个类是必要的,因为 Windows NT 上有一个恼人的错误,即使用默认的 FileSystemView 实例化 JFileChooser 每次都会导致“drive A: not ready”错误。
        我从 1.3 SDK 中获取了 Windows FileSystemView impl 并对其进行了修改,以便 * 不使用 java.io.File.listRoots() 来获取文件系统根。

        java.io.File.listRoots() 执行 SecurityManager.checkRead() 导致操作系统尝试访问驱动器 A:,即使没有磁盘,每次我们都会导致烦人的“abort, retry, ignore”弹出消息实例化一个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;
            }
        

        【讨论】:

          【解决方案4】:

          你可以用这个;

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

          【讨论】:

          • 与 OP 解决方案的唯一区别是 for each 而不是经典的 for 循环 和 getName() 而不是 toString() 对象上的 toString() 方法.您可以解释好处或为什么避免收到错误消息(如询问)。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-23
          • 2013-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 2011-09-14
          相关资源
          最近更新 更多