zoukankan      html  css  js  c++  java
  • java 文件、文件夹复制

    文件复制

    public static void copyFile(String oldPath, String newPath) throws IOException {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                File oldFile = new File(oldPath);
                File file = new File(newPath);
                in = new FileInputStream(oldFile);
                out = new FileOutputStream(file);;
                
                byte[] buffer=new byte[1024];
                int n=0;
                while((n=in.read(buffer))!=-1){
                    out.write(buffer,0,n);
                }
            } finally {
                if(null!=out) {
                    out.close();
                }
                if(null!=in) {
                    in.close();
                }
            }
        }

    文件夹复制

    public static void copyDir(String oldPath, String newPath) throws IOException {
            File oldFile = new File(oldPath);
            File[] listFiles = oldFile.listFiles();
            
            File newFile = new File(newPath);
            if (!newFile.exists()) {
                newFile.mkdir();
            }
            
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    copyDir(file.getPath(), newPath.concat(File.separator).concat(file.getName()));
                }
                
                if (file.isFile()) {
                    copyFile(file.getPath(), newPath.concat(File.separator).concat(file.getName()));
                }
            }
        }
  • 相关阅读:
    BZOJ 3626: [LNOI2014]LCA(树链剖分+离线处理)
    python备用
    STL的使用。。备忘
    DP专题
    任务
    hdu 网络流题集
    hdu KM匹配题集
    hdu 差分约束题集
    hdu 2sat题集
    Codeforces Round #261 (Div. 2)
  • 原文地址:https://www.cnblogs.com/sxf2017/p/9341440.html
Copyright © 2011-2022 走看看