枚举外部驱动器 [英] Enumerate external drives

查看:152
本文介绍了枚举外部驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中,您可以使用 File.listRoots()获取系统中的所有驱动器。

In java, you can use File.listRoots() to get all drives in the system.

我想要只获得外部驱动器,即USB驱动器,外部硬盘,光驱,软盘等。

I'm looking to get only the external drives, i.e. USB drives, external hard disks, optical drives, floppy, etc.

有没有办法在java ?如果没有,本机C ++代码也会很好。在这种情况下,我需要Windows和Linux代码。

Is there any way to do it in java? If not, native C++ code would be good as well. In that case, I need both Windows and Linux code.

推荐答案

要获取文件系统信息, >

To get file system info, use something like:

import java.io.*;
import javax.swing.filechooser.*;

public class DriveTypeInfo
{
  public static void main(String[] args)
  {
      System.out.println("File system roots returned by   FileSystemView.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());
      }
   }
}

http://stackoverflow.com/questions/462110/acquiring-drive-names-as-opposed-to-drive-letters-in-java\"> this ,
这个问题。

Also see this, this and this question.

这篇关于枚举外部驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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