• java 文件复制操作


    本案例采用第三方 jar 包完成,commons-io-2.5.jar,

    这个 jar 对文件操作非常方便,大家可以尝试使用一下。

    这里贴一个简单的 demo 供大家使用

    import java.io.File;
    import java.io.IOException;
    
    import org.apache.commons.io.FileUtils;
    
    public class MyFileUtils {
        public static void main(String[] args) throws IOException {
    
            String srcPath = "F:/copyFile/src";
            String destPath = "F:/copyFile/dest123";
            copyFile(srcPath, destPath);
    
            // copyDirectory(srcPath, destPath);
            // readFileToString(srcPath);
        }
    
        // 创建文件夹
        public static void makeDir(String destPath) {
            File newFilePath = new File(destPath);
            if (!newFilePath.exists()) {
                newFilePath.mkdirs();
            }
        }
    
        // 复制文件夹及其内容
        public static void copyDirectory(String srcPath, String destPath) throws IOException {
            File file = new File(srcPath);
            makeDir(destPath);
            File destFile = new File(destPath);
            FileUtils.copyDirectory(file, destFile);
        }
    
        // 复制文件(应用场景:复制的同时更改文件名称)
        public static void copyFile(String srcPath, String destPath) throws IOException {
            File src = new File(srcPath);
            makeDir(destPath);
            File[] srcFiles = src.listFiles();
            if (srcFiles.length > 0) {
                for (File file : srcFiles) {
                    String filename = file.getName();
                    if (file.isDirectory()) {
                        copyFile(file.getAbsolutePath(), destPath);
                    }
                    if (filename.endsWith("html")) {
                        int index = filename.indexOf(".");
                        String newFileName = filename.substring(0, index) + "复制" + filename.substring(index);
                        File destFile = new File(destPath + "/" + newFileName);
                        FileUtils.copyFile(file, destFile);
                    }
                }
            }
        }
    
        // 把文件 内容转成字符串
        public static void readFileToString(String srcPath) throws IOException {
            File src = new File(srcPath);
            File[] srcFiles = src.listFiles();
            if (srcFiles.length > 0) {
                for (File file : srcFiles) {
                    String filename = file.getName();
                    if (file.isDirectory()) {
                        readFileToString(file.getAbsolutePath());
                    }
                    if (filename.endsWith("html")) {
                        String str = FileUtils.readFileToString(file, "utf-8");
                        System.out.println(str);
                    }
                }
            }
        }
    }

    简单说明一下,

    复制文件,包含文件夹 用到   FileUtils.copyDirectory(file, destFile);  

    复制单个文件  FileUtils.copyFile(file, destFile);

    把文件内容读取为字符串  FileUtils.readFileToString(file, "utf-8");

  • 相关阅读:
    WPF控件操作之改变父控件之TabControl示例
    WPF里面的DockPanel的Fill去哪了,如何填满整个空间
    [原创]winform自定义控件之类属性-多重属性-可折叠属性
    WinForm之DataBinding
    WinForm自定义控件之DefaultValue的误解
    code snippet:依赖属性propa的小技巧
    【原创】WinForm中实现单独Time控件的方式
    node.js安装本地模块遇到的目录锁定问题【新手问题】
    《程序员思维修炼》之德雷福斯模型
    IOptions、IOptionsMonitor以及IOptionsSnapshot
  • 原文地址:https://www.cnblogs.com/stormlong/p/10880198.html
Copyright © 2020-2023  润新知