Java怎么将一个文件夹下的txt文本文档复制到另一个文件夹

Java怎么将一个文件夹下的txt文本文档复制到另一个文件夹

问题描述:

public class test2 {
    public static void main(String[] args) throws IOException {
        File sourcePath = new File("D:\\01");
        File outputPath = new File("D:\\02");

        handlePath(sourcePath, outputPath);
    }
    private static void handlePath(File sourcePath, File outputPath) throws IOException {
        for (File file : sourcePath.listFiles()) { //2.取得源目录下所有的文件或文件夹,并进行遍历
            if (file.isDirectory()) {
//3.如果当前遍历的是文件夹,则使用递归调用,继续处理该文件夹下面的所有文件或文件夹
                handlePath(file, outputPath);
            } else {
//4.如果当前遍历的是不是文件夹而是文件,那么直接开始copy的操作
//4.1.取得文件的文件名,包含文件后缀名
                String fileName = file.getName();
                if (fileName.contains(".txt")) {
//4.2.1.如果该文件有后缀名,即包含“.”点符号,则取得文件的后缀名为:最后一个点符号后面的字符串
                    String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
//4.2.2.根据文件最后的输出的目标目录和文件的后缀名,执行copy的操作(因为最后输出格式为按文件的后缀名分类,即最后输出如:pdf目录下面有pdf文件,txt目录下面有txt文件)
                    copy(file, new File(outputPath, suffix));
                }
            }
        }
    }


    private static void copy(File sourcePath, File outputPath) throws IOException {
        Files.copy(sourcePath.toPath(), outputPath.toPath());
    }
}



```请问要怎么改我现在的代码,现在只能复制出没有后缀的文档,也没有txt的文档名。
![img](https://img-mid.****img.cn/release/static/image/mid/ask/917732524136162.png "=600 #left")

你复制后,是不是没带上文件名后缀txt