在Java中检测多个连接的设备

在Java中检测多个连接的设备,java,list,detect,removable-storage,Java,List,Detect,Removable Storage,我想编写满足以下要求的Java代码: 检测连接到笔记本电脑的多个设备(可移动存储) 列出连接到笔记本电脑的所有设备 允许用户选择Java程序应使用的设备 例如,当有2个USB设备时,Java程序将检测它们,然后将它们列为(例如F:\,g:)。接下来,用户可以选择使用哪个设备。有什么办法吗 我发现这个网站对检测我连接的thumbdrive很有用,但是,它不能检测多个设备 Detect.java public class Detect { public String USBDetect()

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

  • 检测连接到笔记本电脑的多个设备(可移动存储)
  • 列出连接到笔记本电脑的所有设备
  • 允许用户选择Java程序应使用的设备
  • 例如,当有2个USB设备时,Java程序将检测它们,然后将它们列为(例如F:\,g:)。接下来,用户可以选择使用哪个设备。有什么办法吗

    我发现这个网站对检测我连接的thumbdrive很有用,但是,它不能检测多个设备

    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;
        }
    }
    
    此代码帮助我检测多个可移动驱动器,但它也列出了本地驱动器。我认为我应该在Detect.java中包含一些部分,以便它只检测可移动驱动器。至于2。三,。我还没有尝试,因为我不知道如何开始,因为我还没有找到任何相关的网站参考。我希望你能为我提供任何有用的网站或代码,可以满足上述要求。对不起,我是Java新手

    导入java.io.File;
    
    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);
    
        }
    }
    
    导入javax.swing.filechooser.FileSystemView; 公共类检测 { 公共静态void main(字符串[]args) { 检测测试=新检测(); test.USBDetect(); } public void USBDetect() { 字符串driveLetter=“”; FileSystemView fsv=FileSystemView.getFileSystemView(); File[]f=File.listRoots(); 对于(int i=0;i
    导入java.io.File;
    导入javax.swing.filechooser.FileSystemView;
    公共类检测
    {
    公共静态void main(字符串[]args)
    {
    检测测试=新检测();
    test.USBDetect();
    }
    public void USBDetect()
    {
    字符串driveLetter=“”;
    FileSystemView fsv=FileSystemView.getFileSystemView();
    File[]f=File.listRoots();
    对于(int i=0;i
    请发布您尝试过的内容。我已经包含了我当前拥有的代码。通过使用您的代码,我无法获得本地驱动器
    driveLetter
    ,我只获得可移动磁盘
    driveLetter
    当我使用第一个代码时,它只检测到一个可移动驱动器,但当我使用第二个代码时,它检测到所有驱动器,包括C:\和D:\。请发布您尝试的内容。我已包含我当前拥有的代码。通过使用您的代码,我无法获得本地驱动器
    driveLetter
    ,我只得到可移动磁盘
    driveletters
    当我使用第一个代码时,它只检测到一个可移动驱动器,但当我使用第二个代码时,它检测到所有驱动器,包括C:\和D:\。像问题中那样发布相同的代码并不能作为答案!发布与问题中相同的代码并不能作为答案!
    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);
    
        }
    }