使用java获取驱动器信息时 [英] when getting drive information using java

查看:114
本文介绍了使用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 connected and 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,continue when i try continue,it works but how i could avoid that msg

推荐答案

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

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


这个类是必要的,因为在Windows NT上使用默认的 FileSystemView JFileChooser 时会出现烦人的错误c>每次都会导致驱动器A:未准备好错误。

I从1.3 SDK中获取Windows FileSystemView impl并修改它*以便不使用 java.io.File.listRoots()获取fileSystem根目录。

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()执行 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获取驱动器信息时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆