Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中扫描所有驱动器以查找特定的文件扩展名?_Java - Fatal编程技术网

如何在Java中扫描所有驱动器以查找特定的文件扩展名?

如何在Java中扫描所有驱动器以查找特定的文件扩展名?,java,Java,我想扫描电脑中的所有磁盘,找到记事本(.txt)文件,并将它们复制到新创建的目录中。怎么做?如果可能的话,跳过C类Windows下的一些文件夹。目前我只能扫描特定的目录 谢谢 public void scanFolder(int depth, File file) { StringBuilder indent = new StringBuilder(); String name = file.getName(); String extension;

我想扫描电脑中的所有磁盘,找到记事本(.txt)文件,并将它们复制到新创建的目录中。怎么做?如果可能的话,跳过C类Windows下的一些文件夹。目前我只能扫描特定的目录

谢谢

         public void scanFolder(int depth, File file) {

    StringBuilder indent = new StringBuilder();
    String name = file.getName();
    String extension;
    String txtcheck = "txt";
)

for(int i=0;i
我已经完成了,下面是答案代码

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import javax.swing.filechooser.FileSystemView;

 public class Find {

private static int finalTotal = 0;

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);
        finalTotal = finalTotal + 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;
    }
}

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

    File[] paths;
    FileSystemView fsv = FileSystemView.getFileSystemView();

    paths = File.listRoots();

    for (File path : paths) {
        String str = path.toString();
        String slash = "\\";

        String s = new StringBuilder(str).append(slash).toString();

        Path startingDir = Paths.get(s);

        String pattern = "*.txt";

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

    }

    System.out.println("Total Matched Number of Files : " + finalTotal);

}
import java.io.*;
导入java.nio.file.*;
导入java.nio.file.attribute.*;
导入静态java.nio.file.FileVisitResult.*;
导入javax.swing.filechooser.FileSystemView;
公共类查找{
私有静态int finalTotal=0;
公共静态类查找器
扩展SimpleFileVisitor{
私有最终路径匹配器匹配器;
私有整数匹配=0;
查找器(字符串模式){
matcher=FileSystems.getDefault()
.getPathMatcher(“全局:+模式);
}
//将glob模式与
//文件或目录名。
无效查找(路径文件){
路径名=file.getFileName();
if(name!=null&&matcher.matches(name)){
numMatches++;
System.out.println(文件);
}
}
//打印的总数量
//匹配到标准输出。
作废完成(){
System.out.println(“匹配:”
+numMatches);
finalTotal=finalTotal+数值匹配;
}
//调用模式匹配
//方法在每个文件上。
@凌驾
公共文件VisitResult visitFile(路径文件,
基本文件属性(属性属性){
查找(文件);
继续返回;
}
//调用模式匹配
//方法在每个目录上。
@凌驾
公共文件VisitResult preVisitDirectory(路径目录,
基本文件属性(属性属性){
查找(目录);
继续返回;
}
@凌驾
公共文件VisitResult visitFileFailed(路径文件,
IOEXC(例外情况){
//系统错误打印项次(exc);
继续返回;
}
}
公共静态void main(字符串[]args)
抛出IOException{
文件[]路径;
FileSystemView fsv=FileSystemView.getFileSystemView();
路径=File.listRoots();
用于(文件路径:路径){
String str=path.toString();
字符串斜杠=“\\”;
字符串s=新的StringBuilder(str).append(斜杠).toString();
Path startingDir=Path.get;
字符串模式=“*.txt”;
查找器=新查找器(模式);
walkFileTree(startingDir,finder);
finder.done();
}
System.out.println(“匹配的文件总数:“+finalTotal”);
}

}

你说的所有磁盘是什么意思?。你说的是所有驱动器吗?只要在stackoverflow中搜索一下,就会得到这些链接。调查
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import javax.swing.filechooser.FileSystemView;

 public class Find {

private static int finalTotal = 0;

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);
        finalTotal = finalTotal + 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;
    }
}

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

    File[] paths;
    FileSystemView fsv = FileSystemView.getFileSystemView();

    paths = File.listRoots();

    for (File path : paths) {
        String str = path.toString();
        String slash = "\\";

        String s = new StringBuilder(str).append(slash).toString();

        Path startingDir = Paths.get(s);

        String pattern = "*.txt";

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

    }

    System.out.println("Total Matched Number of Files : " + finalTotal);

}