2009-08-19 70 views
2

驅動器的信息,當我試圖下面的程序獲得使用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); 
     } 
    } 
} 

,但在我的系統軟盤驅動器沒有連接 和我得到類似如下

消息「驅動器未準備好請檢查驅動器A:並確保磁盤已插入,並且驅動器門已關閉「 然後顯示三個選項取消,再試一次,繼續 當我嘗試繼續時,它的工作原理 但我怎麼可以避免那msg

回答

2

你想做什麼?

我的建議是使用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/

2

當涉及到Windows,這class WindowsAltFileSystemView提出了一種基於的FileSystemView

替代

由於在Windows NT上使用默認的FileSystemView實例化JFileChooser會每次都會導致「drive A: not ready」錯誤,所以此類是必需的。
我從1.3 SDK中抓取了Windows FileSystemView impl,並對其進行了修改,以便不使用java.io.File.listRoots()來獲取fileSystem根目錄。

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