2010-08-22 39 views
16

Làm thế nào tôi có thể liệt kê tất cả các ổ đĩa mà còn nhận được loại ổ đĩa tương ứng (có thể tháo rời, ổ đĩa cục bộ hoặc đĩa CD-rom, dvd-rom ... vv)?Làm thế nào tôi có thể lấy danh sách tất cả các ổ đĩa mà còn nhận được loại ổ tương ứng (ổ đĩa di động, đĩa cục bộ hoặc đĩa CD-rom, dvd-rom ... vv)?

+0

là này trong một môi trường cửa sổ? – Woot4Moo

Trả lời

3

Không có định nghĩa cho những gì bạn đang yêu cầu.

Tôi có ổ USB, khi tôi cắm nó vào, hiển thị dưới dạng đĩa CD-ROM. Sau khi tôi chạy một chương trình trên đĩa CD-ROM nó gắn một phân vùng thứ hai mà dường như là một ổ đĩa cứng.

Tôi có ổ đĩa CD-ROM có thể tháo rời. Tôi cũng có ổ cứng "bên trong" eSATA bên ngoài máy tính của mình.

Bạn sẽ phải làm việc khá chăm chỉ để có được định nghĩa ràng buộc cho "loại" của biến tần. Tại sao bạn không cho chúng tôi biết những gì bạn đang cố gắng làm, thay vì hỏi về cách cụ thể bạn muốn làm điều đó?

+0

Cảm ơn bạn đã trả lời! Tôi giả định rằng tất cả các ổ đĩa CD/DVD đều có thể tháo rời được, tôi cũng muốn biết là một số ổ đĩa được kết nối qua USB để giả sử nó có thể “tháo rời” được. Các thư mục con (ed) cũng có thể "tháo rời" được. Vì vậy, trong đơn giản, tôi muốn biết một người dùng có thể rút một số ổ đĩa dễ dàng hay không. – xolmc

9

Giả sử cửa sổ của nó, hãy sử dụng File.listRoots() để lấy tất cả các gốc. Sau đó sử dụng FileSystemView để kiểm tra xem đó là đĩa mềm hay ổ đĩa. Khác hơn là tôi không có ý tưởng.

21

Với mã này, bạn có thể nhận được tất cả các ổ đĩa và mô tả kiểu của nó

File[] paths; 
FileSystemView fsv = FileSystemView.getFileSystemView(); 

// returns pathnames for files and directory 
paths = File.listRoots(); 

// for each pathname in pathname array 
for(File path:paths) 
{ 
    // prints file and directory paths 
    System.out.println("Drive Name: "+path); 
    System.out.println("Description: "+fsv.getSystemTypeDescription(path)); 
} 
+1

Xin lưu ý rằng các mô tả phụ thuộc vào ngôn ngữ. –

+1

Mã tốt nhưng làm thế nào để bạn có được loại ổ đĩa theo ngôn ngữ độc lập? – BullyWiiPlaza

2

Nếu bạn sử dụng JACOB, bạn có thể liệt kê tất cả các ổ đĩa cùng với các loại thích hợp: sản lượng

import com.jacob.activeX.ActiveXComponent; 
import com.jacob.com.Dispatch; 
import com.jacob.com.EnumVariant; 
import com.jacob.com.JacobObject; 
import com.jacob.com.Variant; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

public class DrivesExample 
{ 
    public interface HasNativeValue 
    { 
     int getNativeValue(); 
    } 

    public enum DriveTypeEnum implements HasNativeValue 
    { 
     Unknown(0), 
     NoRootDirectory(1), 
     RemovableDisk(2), 
     LocalDisk(3), 
     NetworkDrive(4), 
     CompactDisc(5), 
     RAMDisk(6); 

     public final int nativeValue; 

     DriveTypeEnum(int nativeValue) 
     { 
      this.nativeValue = nativeValue; 
     } 

     public int getNativeValue() 
     { 
      return nativeValue; 
     } 
    } 

    private static <T extends Enum<T> & HasNativeValue> T fromNative(Class<T> clazz, int value) 
    { 
     for (T c : clazz.getEnumConstants()) 
     { 
      if (c.getNativeValue() == value) 
      { 
       return c; 
      } 
     } 
     return null; 
    } 

    /** 
    * The drive information. 
    */ 
    public static final class Drive 
    { 
     /** 
     * File system on the logical disk. Example: NTFS. null if not known. 
     */ 
     public final String fileSystem; 
     /** 
     * Value that corresponds to the type of disk drive this logical disk represents. 
     */ 
     public final DriveTypeEnum driveType; 
     /** 
     * The Java file, e.g. "C:\". Never null. 
     */ 
     public final File file; 

     public Drive(String fileSystem, DriveTypeEnum driveType, File file) 
     { 
      this.fileSystem = fileSystem; 
      this.driveType = driveType; 
      this.file = file; 
     } 

     @Override 
     public String toString() 
     { 
      return "Drive{" + file + ": " + driveType + ", fileSystem=" + fileSystem + "}"; 
     } 
    } 

    /** 
    * Lists all available Windows drives without actually touching them. This call should not block on cd-roms, floppies, network drives etc. 
    * 
    * @return a list of drives, never null, may be empty. 
    */ 
    public static List<Drive> getDrives() 
    { 
     List<Drive> result = new ArrayList<>(); 
     ActiveXComponent axWMI = new ActiveXComponent("winmgmts://"); 

     try 
     { 
      Variant devices = axWMI.invoke("ExecQuery", new Variant("Select DeviceID,DriveType,FileSystem from Win32_LogicalDisk")); 
      EnumVariant deviceList = new EnumVariant(devices.toDispatch()); 
      while (deviceList.hasMoreElements()) 
      { 
       Dispatch item = deviceList.nextElement().toDispatch(); 
       String drive = Dispatch.call(item, "DeviceID").toString().toUpperCase(); 
       File file = new File(drive + "/"); 
       DriveTypeEnum driveType = fromNative(DriveTypeEnum.class, Dispatch.call(item, "DriveType").getInt()); 
       String fileSystem = Dispatch.call(item, "FileSystem").toString(); 
       result.add(new Drive(fileSystem, driveType, file)); 
      } 

      return result; 
     } finally 
     { 
      closeQuietly(axWMI); 
     } 
    } 

    private static void closeQuietly(JacobObject jacobObject) 
    { 
     try 
     { 
      jacobObject.safeRelease(); 
     } catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    public static void main(String[] arguments) 
    { 
     List<Drive> drives = getDrives(); 

     for (Drive drive : drives) 
     { 
      System.out.println(drive.toString()); 
     } 
    } 
} 

Ví dụ:

Drive{C:\: LocalDisk, fileSystem=NTFS} 
Drive{D:\: LocalDisk, fileSystem=NTFS} 
Drive{E:\: RemovableDisk, fileSystem=NTFS} 
Drive{F:\: RemovableDisk, fileSystem=FAT32} 
Drive{G:\: RemovableDisk, fileSystem=null} 
Drive{Y:\: NetworkDrive, fileSystem=NTFS} 

Để sử dụng JACOB, hãy thêm JARDLL từ tệp tải xuống dưới dạng thư viện trong dự án của bạn. Giải pháp này chỉ là Windows.

-1
File[] roots = File.listRoots(); 
for(int i=0;i<roots.length;i++) 
    System.out.println("Root["+i+"]:" + roots[i]); 
+0

Điều này không in/chứa loại ổ đĩa – BullyWiiPlaza

0

Đoạn mã này hoạt động trên Windows. Nó chính xác báo cáo ổ đĩa USB bút là có thể tháo rời, nhưng trên máy tính xách tay của tôi một ổ đĩa cứng USB được đánh dấu là không thể tháo rời. Dù sao đây là tốt nhất tôi tìm thấy mà không sử dụng mã nguồn gốc.

for (Path root : FileSystems.getDefault().getRootDirectories()) { 
    FileStore fileStore = Files.getFileStore(root); 
    System.out.format("%s\t%s\n", root, fileStore.getAttribute("volume:isRemovable")); 
} 

Nguồn: this file that apparently comes from the JDK

Các vấn đề liên quan