-1

我编写了一个程序,通过传递其扩展名来查找指定类型的所有文件。我的问题是,程序仅在 C 驱动器中查找文件,但我想搜索整个硬盘。这是我的程序示例

public class Find {

public static class Finder extends SimpleFileVisitor<Path> {

    private final PathMatcher matcher;
    private int numMatches = 0;

    Finder(String pattern) 
    {
        matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
    }

    // Compares the glob pattern against
    // the file or directory name.
    void find(Path file) 
    {
        Path name = file.getFileName();
        if (name != null && matcher.matches(name)) 
        {
            numMatches++;
            System.out.println(file);
        }
    }

    // Prints the total number of
    // matches to standard out.
    void done() 
    {
        System.out.println("Matched: "+ numMatches);
    }

    // Invoke the pattern matching
    // method on each file.
    //@Override
    public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) 
    {
        find(file);
        return CONTINUE;
    }

    // Invoke the pattern matching
    // method on each directory.
    //@Override
    public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) 
    {
        find(dir);
        return CONTINUE;
    }

    //@Override
   public FileVisitResult visitFileFailed(Path file,IOException exc) 
    {
        System.err.println(exc);
        return CONTINUE;
    }
}

static void usage()
{
    System.err.println("java Find <path>" +" -name \"<glob_pattern>\"");
    System.exit(-1);
}

public static void main(String[] args)throws IOException 
{

    if (args.length < 2 ) 
    {
        usage();
    } 
    Path startingDir = Paths.get(args[0]);
    String pattern = args[1];

    Finder finder = new Finder(pattern);
    Files.walkFileTree(startingDir, finder);
    finder.done();
}

}

4

2 回答 2

2

您可以使用该File.listRoots()方法查找 Windows 上的所有驱动器。之后,只需在每个驱动器上进行独立搜索。

使用新的 API (java.nio.file) 还有另一种方法:FileSystem.getDefault().getRootDirectories().

for (Path startingDir : FileSystem.getDefault().getRootDirectories()) {
    // find files here
}
于 2013-07-26T06:21:33.530 回答
1

Try something like this

  File folder = new File("D:\\DestFile");
  File[] listOfFiles = folder.listFiles();

  for (File file : listOfFiles) {
   if (file.isFile()&&   (file.getName().substring(file.getName().lastIndexOf('.')+1).equals("your_type"))) {// txt or docx or something
     // do something
    }
    }

Try this. you can read all file in your PC by this

    public static void main(String[] args) {
    File[] paths = File.listRoots();
    for(File directory:paths){
       getFile(directory.toString());
    }
}

public static void getFile(String directoryName) {
    File directory = new File(directoryName);
    File[] fList = directory.listFiles();
    if(fList!=null){
        for (File file : fList) {
            if (file.isFile()) {
                System.out.println(file.toString());
            } else if (file.isDirectory()) {
                getFile(file.getAbsolutePath());
            }
        }
    }

}
于 2013-07-26T06:40:41.547 回答