2

我想编写满足以下要求的 Java 代码:

  1. 检测连接到笔记本电脑的多个设备(可移动存储)
  2. 列出连接到笔记本电脑的所有设备
  3. 允许用户选择 Java 程序应使用的设备

例如,当有 2 个 USB 设备时,Java 程序会检测到它们,然后将它们列为 (eg F:\, G:)。在此之后,用户可以选择使用哪个设备。有什么办法吗?

我发现这个网站http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java对检测我连接的拇指驱动器很有用,但是,它无法检测到多个设备.

检测.java

public class Detect
{
    public String USBDetect()
    {
        String driveLetter = "";
        FileSystemView fsv = FileSystemView.getFileSystemView();

        File[] f = File.listRoots();
        for (int i = 0; i < f.length; i++)
        {
            String drive = f[i].getPath();
            String displayName = fsv.getSystemDisplayName(f[i]);
            String type = fsv.getSystemTypeDescription(f[i]);
            boolean isDrive = fsv.isDrive(f[i]);
            boolean isFloppy = fsv.isFloppyDrive(f[i]);
            boolean canRead = f[i].canRead();
            boolean canWrite = f[i].canWrite();

            if (canRead && canWrite && !isFloppy && isDrive && (type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile")))
            {
                //log.info("Detected PEN Drive: " + drive + " - "+ displayName); 
                driveLetter = drive;
                break;
            }
        }

        /*if (driveLetter.equals(""))
        {
            System.out.println("Not found!");
        } 
        else 
        {
            System.out.println(driveLetter);
        }
        */

        //System.out.println(driveLetter);
        return driveLetter;
    }
}

列表.java

public class List 
{
    public static void main(String[] args) 
    {   
        File[] units = File.listRoots();

        for(File unit:units)
        {
            System.out.println(unit.getAbsolutePath());
        }
    }
}

此代码帮助我检测超过 1 个可移动驱动器,但它还列出了本地驱动器。我相信我应该包含 Detect.java 的某些部分,以便它仅检测可移动驱动器。至于2.和3.我还没有尝试过,因为我不知道如何开始,因为我没有找到任何相关的网站可以参考。希望大家能给我提供任何可以满足上述要求的有用的网站或代码。抱歉,我是 Java 新手。

4

1 回答 1

-1
import java.io.File;
import javax.swing.filechooser.FileSystemView;

public class Detect
{
    public static void main(String[] args)
    {
        Detect test = new Detect();
        test.USBDetect();
    }

    public void USBDetect()
    {
        String driveLetter = "";
        FileSystemView fsv = FileSystemView.getFileSystemView();

        File[] f = File.listRoots();
        for (int i = 0; i < f.length; i++)
        {
            String drive = f[i].getPath();
            String displayName = fsv.getSystemDisplayName(f[i]);
            String type = fsv.getSystemTypeDescription(f[i]);
            boolean isDrive = fsv.isDrive(f[i]);
            boolean isFloppy = fsv.isFloppyDrive(f[i]);
            boolean canRead = f[i].canRead();
            boolean canWrite = f[i].canWrite();

            if (canRead && canWrite && !isFloppy && isDrive && (type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile")))
            {
                //log.info("Detected PEN Drive: " + drive + " - "+ displayName); 
                driveLetter = drive;              
                System.out.println(driveLetter);
            }
        }

        /*if (driveLetter.equals(""))
        {
            System.out.println("Not found!");
        } 
        else 
        {
            System.out.println(driveLetter);
        }*/

        //System.out.println(driveLetter);

    }
}
于 2016-05-06T14:21:34.997 回答