2009-08-19 11 views
2

i das folgende Programm versuchtwenn Laufwerk Informationen bekommen 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); 
     } 
    } 
} 

aber in meinem System-Diskettenlaufwerk ist nicht verbunden und ich bin eine Nachricht wie die folgende

"Der Antrieb immer nicht bereit ist für die Verwendung, seine Tür kann offen sein, überprüfen Sie bitte Laufwerk A: und stellen Sie sicher, dass die Festplatte eingelegt ist und dass die Laufwerkstür geschlossen ist " dann drei Optionen angezeigt werden Abbrechen, versuchen Sie es erneut, weiter , wenn ich versuche, es funktioniert aber wie könnte ich das vermeiden msg

Antwort

2

Was möchten Sie tun?

Meine Empfehlung wäre FileSystemView zu verwenden.

es so etwas wie diese verwendet wird:

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

}

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

2

Wenn es um Windows enthalten ist, diese class WindowsAltFileSystemView schlägt eine alternative basiert auf Filesystem

Diese Klasse ist aufgrund eines lästigen Fehlers in Windows NT erforderlich, bei dem die Instanziierung eines JFileChooser mit dem Standard FileSystemView jedes Mal einen Fehler "drive A: not ready" verursacht.
Ich packte die Windows FileSystemView impl aus dem 1.3 SDK und änderte es so * als java.io.File.listRoots() nicht zu verwenden, um FileSystem Roots zu erhalten.

java.io.File.listRoots() hat eine SecurityManager.checkRead(), die das Betriebssystem den Zugriff auf Laufwerk versuchen verursacht A: selbst dann, wenn keine Platte dort ist, was eine lästige „abort, retry, ignore“ Popup-Meldung jedes Mal instanziiert wir eine JFileChooser!

So, hier ist die Idee FileSystemView zu erstreckt, mit den getRoots() Verfahren ersetzt:

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