简体   繁体   English

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

[英]Find a specific file in a detected USB from Java

I am using Java codes to find a file that ends with a certain extension in a detected removable storage. 我正在使用Java代码在检测到的可移动存储中找到以某个扩展名结尾的文件。 I am trying to link the two codes together but I am not sure on how I can do so. 我正在尝试将两个代码链接在一起,但不确定如何实现。 These are the codes I am using: 这些是我正在使用的代码:

DetectDrive.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;
    }
}

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

The file that I want the program to search ends with a .raw extension and I want the program to search for the file in the detected removable storage (eg F:). 我希望程序搜索的文件以.raw扩展名结尾,并且我希望程序搜索检测到的可移动存储(例如F :)中的文件。 How do I link these 2 codes together? 如何将这两个代码链接在一起? If possible I would like an example of codes to link them. 如果可能的话,我想要一个链接它们的代码示例。 I got the codes for FileSearch.java from http://rosettacode.org/wiki/Walk_a_directory/Recursively#Java 我从http://rosettacode.org/wiki/Walk_a_directory/Recursively#Java获得了FileSearch.java的代码

First, change USBDetect by replacing String driveLetter with File removableDrive , and return that. 首先,改变USBDetect替换String driveLetterFile removableDrive ,并返回。 Then pass the value returned from USBDetect into find . 然后将USBDetect返回的值传递给find

Heres how I would do it, however I would also make the methods USBDetect and find static, they both dont seem to have any objects their referencing in their parent class. 继承人我会怎么做,但我也会使方法USBDetectfind静态的,他们俩好像还没有任何物体的引用在他们的父类。 Also make USBDetect return a File instead of a String 还要使USBDetect返回File而不是String

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+":"));
    }
}

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

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