在从Java检测到的USB中查找特定文件

在从Java检测到的USB中查找特定文件,java,file-search,removable-storage,Java,File Search,Removable Storage,我使用Java代码在检测到的可移动存储中查找以某个扩展名结尾的文件。我正在尝试将这两个代码链接在一起,但我不确定如何才能做到这一点。以下是我正在使用的代码: DetectDrive.java import java.io.*; import java.util.*; import javax.swing.filechooser.FileSystemView; public class DetectDrive { public String USBDetect() {

我使用Java代码在检测到的可移动存储中查找以某个扩展名结尾的文件。我正在尝试将这两个代码链接在一起,但我不确定如何才能做到这一点。以下是我正在使用的代码:

DetectDrive.java

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

public class DetectDrive
{
    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;
    }
}
fileSearch.java

import java.io.*;

public class FileSearch
{   
    public String find(File dir) 
    {
        String pattern = ".raw";

        File listFile[] = dir.listFiles();
        if (listFile != null) 
        {
            for (int i=0; i<listFile.length; i++) 
            {
                if (listFile[i].isDirectory()) 
                {
                    find(listFile[i]);
                } else 
                { 
                    if (listFile[i].getName().endsWith(pattern)) 
                    {
                        System.out.println(listFile[i].getPath());
                    }
                }
            }
        }
        return pattern;
    }
}

我希望程序搜索的文件以.raw扩展名结尾,我希望程序在检测到的可移动存储中搜索该文件,例如F:。如何将这两个代码链接在一起?如果可能的话,我想要一个代码的例子来链接它们。我首先从

中获得了FileSearch.java的代码,通过将String drivelett替换为File removableDrive来更改USBDetect,然后返回该代码。然后将从USBDetect返回的值传递到find中。

我将这样做,但是我也会使USBDetect和find方法成为静态的,它们的父类中似乎都没有引用的对象。还要使USBDetect返回一个文件而不是字符串


当我将其更改为File时,公共字符串usbdetec必须更改为公共文件usbdetec,对吗?是的。您还需要删除驱动器变量并更改driveLetter=drive;移除驱动器=f[i];如果可移动驱动器的盘符未固定,会发生什么情况?例如,在一台计算机中可能是F,在另一台计算机中可能是G。是字符串驱动器吗?@user3847620抱歉,刚刚修复了它。F不应该出现在那里我已经尝试过你的解决方案,效果非常好!谢谢昨天它在显示驱动器和文件时工作得很好,但是当我今天再次尝试运行它时,输出显示程序已终止,没有任何错误。我没有对代码做任何更改。这是否意味着程序有问题?可能,你应该调试你的程序。开始调试的一个好方法是在程序周围放置一些System.out.println语句,以帮助您了解为什么它不再工作。一些好的开始位置是:驱动器找不到吗?文件找不到吗?
public static void main(String [] args) {
    // look for the drive
    String drive = (new DetectDrive()).USBDetect();
    // if it found a drive (null or empty string says no)
    if(drive != null && !drive.isEmpty()) {
        // look for a file in that drive
        FileSearch fileSearch = new FileSearch();
        fileSearch.find(new File(drive+":"));
    }
}