本文介绍了使用java获取驱动器信息时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下程序

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 connectedand i am getting a message like the following

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

" 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,continuewhen i try continue,it worksbut how i could avoid that msg

推荐答案

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

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

java.io.File.listRoots()执行 SecurityManager。 checkRead()这导致操作系统尝试访问驱动器 A:即使没有磁盘,也会导致烦人的 JFileChooser 时,code> abort,retry,ignore 弹出消息!

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!

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

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

这篇关于使用java获取驱动器信息时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:59
查看更多