简体   繁体   English

用Java检测多个连接的设备

[英]Detect multiple connected devices in Java

I want to write Java codes that will meet the following requirements: 我想编写满足以下要求的Java代码:

  1. Detect multiple devices (removable storage) connected to the laptop 检测连接到笔记本电脑的多个设备(可移动存储)
  2. List down all devices connected to the laptop 列出所有连接到笔记本电脑的设备
  3. Allow users to choose which device that should be used by the Java program 允许用户选择Java程序应使用的设备

For example, when there are 2 USB devices, the Java program will detect them and then list them as (eg F:\\, G:). 例如,当有2个USB设备时,Java程序将检测到它们,然后将它们列出为(例如F:\\,G :)。 Following this, the users can choose which device to use. 之后,用户可以选择要使用的设备。 Is there any way to do so? 有什么办法吗?

I found this website http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java useful to detect my thumbdrive that is connected, however, it is not able to detect more than one device. 我发现此网站http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java对检测已连接的拇指驱动器很有用,但是,它不能检测多个设备。

Detect.java Detect.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;
    }
}

List.java List.java

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

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

This code helps me detect more than 1 removable drive but it also lists down the local drives. 这段代码可以帮助我检测到多个可移动驱动器,但同时也列出了本地驱动器。 I believe I should include in some parts from the Detect.java for it to detect only removable drives. 我相信我应该在Detect.java的某些部分中包括它,以便仅检测可移动驱动器。 As for 2. and 3. I have not tried yet because I do not know how to start, since I have not found any relevant websites to reference to. 至于2.和3.,我还没有尝试过,因为我不知道如何开始,因为我没有找到任何相关的网站可以参考。 I hope that you can provide me with any useful websites or codes that could meet the requirements above. 希望您可以向我提供可以满足上述要求的任何有用的网站或代码。 Sorry I am new to Java. 抱歉,我是Java新手。

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM