用Java检测多个连接的设备

时间:2014-07-27 06:01:39

标签: java list detect removable-drive

我想编写符合以下要求的Java代码:

  1. 检测连接到笔记本电脑的多个设备(可移动存储设备)
  2. 列出连接到笔记本电脑的所有设备
  3. 允许用户选择Java程序应该使用的设备
  4. 例如,当有2个USB设备时,Java程序将检测它们,然后将它们列为(例如F:\,G :)。在此之后,用户可以选择使用哪个设备。有没有办法这样做?

    我发现此网站http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-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

    public class List 
    {
        public static void main(String[] args) 
        {   
            File[] units = File.listRoots();
    
            for(File unit:units)
            {
                System.out.println(unit.getAbsolutePath());
            }
        }
    }
    

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

1 个答案:

答案 0 :(得分:-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);

    }
}