Java通过指定文件夹下的指定文件类型复制到指定的文件夹下

java 文件复制

Java复制文件有多种方式,本文只列举了其中的一种。
该程序的效果是:获取指定文件夹下的指定类型的文件,然后复制到指定的文件夹下。

PS:该程序一次只能复制一种类型
public class CopyFileExample {
    public static void main(String[] args) throws IOException {
        //获取键盘的输入
        BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入原文件路径:");
        String oldPath = buffer.readLine();
        System.out.println("请输入复制后路径:");
        //该路径不能在源文件的路径下,否则就会把newPath目录下的文件覆盖,大小为0kb。
        String newPath = buffer.readLine()+"\\";
        System.out.println("请输入复制的文件类型:");
        String fileType = buffer.readLine();

        File files=new File(oldPath);
        getAllFile(files,newPath,fileType);
        System.out.println("复制成功");
    }

    //获取文件名称的方法
    public static void getAllFile(File file,String newPath,String fileType){
        File[] files = file.listFiles();
        for (File fileName : files) {
            if(fileName.isDirectory()){
                //如果是文件夹
                getAllFile(fileName,newPath,fileType);
            }else{
                //如果是文件
                //获取文件的绝对路径
                String name = fileName.getAbsolutePath();
                if(name.endsWith(".md")){
                    //通过获取的路径进行文件的复制
                    //调用复制的方法
                    fileCopy(name,newPath);
                }
            }
        }
    }
    //文件复制方法
    public static void fileCopy(String str,String newPath){
        //获取文件名称
        String substring = str.trim().substring(str.lastIndexOf("\\") + 1);
        FileReader fr=null;
        FileWriter fw=null;
        try {
            fr=new FileReader(str);
            fw=new FileWriter(newPath+substring);

            int len=0;
            char[] ch=new char[1024];
            while((len=fr.read(ch))!=-1){
                fw.write(ch,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //防止空指针异常
            if(fw!=null&&fr!=null){
                try {
                    fw.close();
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

如有问题欢迎指正
https://pixabay.com

Java编程中,你可以使用`java.io.File`类以及`java.nio.file.Files`包下的功能来实现将一个文件夹下所有图片文件复制到另一个指定位置。以下是一个简单的示例: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CopyImages { public static void copyFolderToAnother(String sourceDirPath, String destinationDirPath) { try { // 获取源目录路径 Path source = Paths.get(sourceDirPath); // 检查源目录是否存在并且是文件夹 if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source directory does not exist or is not a folder."); } // 创建目标目录(如果不存在) Files.createDirectories(Paths.get(destinationDirPath)); // 遍历源目录中的所有文件 for (Path entry : Files.newDirectoryStream(source)) { // 检查是否为图像文件,假设我们只关心.jpg和.png if (entry.toString().endsWith(".jpg") || entry.toString().endsWith(".png")) { // 获取文件名和目标路径 String fileName = entry.getFileName().toString(); Path targetFile = Paths.get(destinationDirPath, fileName); // 执行复制操作 Files.copy(entry, targetFile); System.out.println("Copied file " + fileName + " to " + targetFile); } } } catch (IOException e) { System.err.println("Error copying files: " + e.getMessage()); } } public static void main(String[] args) { String source = "/path/to/source/folder"; String destination = "/path/to/destination/folder"; copyFolderToAnother(source, destination); } } ``` 在这个示例中,你需要替换`source`和`destination`变量为你实际的文件夹路径。这个脚本会查找并复制源目录下的所有`.jpg`和`.png`文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值