io流文件夹及文件复制,出现文件夹子文件和文件夹同在一个目录

weixin_50996685 2021-03-24 08:46:06
import java.io.*;

public class FileDirTest01 {
public static void main(String[] args) {
// 拷贝文件夹目录
File in = new File("");
// 拷贝目标(目录)
File out = new File("");
// 调用方法
CopyDir(in, out);
}

/**
* 拷贝文件夹里面文件夹及文件,
* @param in 拷贝文件目录
* @param out 拷贝目标(目录)
*/
private static void CopyDir(File in, File out) {
if(in.isFile()) {
// srcFile如果是一个文件的话,递归结束。
// 是文件的时候需要拷贝。
// ....一边读一边写。
FileInputStream input = null;
FileOutputStream output = null;
try {
// 读这个文件
input = new FileInputStream(in);
// 写文件到新的目录为 out.getAbsolutePath()+"\\"+in.getName()
output = new FileOutputStream(out.getAbsolutePath()+"/"+in.getName());
byte[] bytes = new byte[1024*1024]; //一次复制1M
int readCount = 0;
/**
* 复制文件 一次复制1024*1024字节(1M)
*/
while((readCount = input.read(bytes)) != -1) {
output.write(bytes,0,readCount);
}
output.flush();

} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
File[] files = in.listFiles();
for (File file: files) {
if (file.isDirectory()) { //判断是否为文件夹,是
// 新的目录为 out.getAbsolutePath() + "/" + in.getName()
File newDir = new File(out.getAbsolutePath()+"\\"+in.getName());
if (!newDir.exists()) { //判断是否含有此文件夹,否
newDir.mkdirs(); //创建子目录
}
CopyDir(file,newDir);
System.out.println(newDir.getAbsolutePath());
System.out.println(file.getAbsolutePath());
}
// 递归方法CopyDir
CopyDir(file,out);
}
}
}
...全文
351 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2021-04-08
  • 打赏
  • 举报
回复
一共就有两件事: 1. 遍历源文件夹 2. 复制文件(夹) 编程思路也可以这样安排,遍历采用递归调用的函数形式,复制文件是IO操作的函数。怎样将两者融合起来呢? 可以采用参数回调的方式,写一个接口作为回调参数的规范,然后,编写实现类即可。 上代码:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;


public class PathCopy {

    /** 遍历文件夹时,针对遍历的对象进行回调访问 */
    interface Visitor {
        void handle(File file) throws IOException;
    }

    /** 1. 遍历文件夹 */
    public static void scan(File path, Visitor visitor) throws IOException {
        if (path.exists()) {
            for (File file : path.listFiles()) {
                visitor.handle(file);
                if (file.isDirectory()) {
                    scan(file, visitor);
                }
            }
        }
    }

    /** 2. 文件拷贝 */
    public static void copy(File src, File des) throws IOException {
        RandomAccessFile target = new RandomAccessFile(des, "rwd");
        RandomAccessFile source = new RandomAccessFile(src, "r");
        try (FileChannel out = target.getChannel();
            FileChannel in = source.getChannel()) {
            out.transferFrom(in, 0, src.length());
        }
    }

    public static void pathCopy(File srcFolder, File desFolder) throws IOException {
        if (!desFolder.exists()) {
            desFolder.mkdirs();
        }
        final String basePath = srcFolder.getPath();
        scan(srcFolder, file -> {
            String parent = srcFolder.getParent();
            String relative = parent.substring(basePath.length());
            File target = new File(desFolder,relative + "/" + file.getName());
            if (file.isDirectory()) {
                target.mkdirs();
            } else {
              copy(file, target);  
            }
        });
    }

    public static void main(String[] args) throws IOException {
        final File srcFolder = new File("src/path");
        final File desFolder = new File("des/path");
        pathCopy(srcFolder, desFolder);
    }

}
a5156520 2021-04-07
  • 打赏
  • 举报
回复
只能把文件目录test下的内容复制到test2目录下,不包括test文件夹


import java.io.*;

public class FileDirTest01 {
    public static void main(String[] args) {
//        // 拷贝文件夹目录
        File in = new File("/users/lifeisgood/Documents/test");
        // 拷贝目标(目录)
        File out = new File("/users/lifeisgood/Documents/test2");
        //  调用方法
        CopyDir(in, out);
        
//    	File in = new File("/users/lifeisgood/Documents/test");
//    	System.out.println("getName="+in.getName());
//    	System.out.println("getAbsolutePath()="+in.getAbsolutePath());
    }

    /**
     * 拷贝文件夹里面文件夹及文件,
     * @param in    拷贝文件目录
     * @param out   拷贝目标(目录)
     */
    private static void CopyDir(File in, File out) {
        if(in.isFile()) {
            // srcFile如果是一个文件的话,递归结束。
            // 是文件的时候需要拷贝。
            // ....一边读一边写。
            FileInputStream input = null;
            FileOutputStream output = null;
            try {
                // 读这个文件
                input = new FileInputStream(in);
                // 写文件到新的目录为 out.getAbsolutePath()+"\\"+in.getName()
                output = new FileOutputStream(out.getAbsolutePath()+"/"+in.getName());
                byte[] bytes = new byte[1024*1024];     //一次复制1M
                int readCount = 0;
                /**
                 *        复制文件    一次复制1024*1024字节(1M)
                 */
                while((readCount = input.read(bytes)) != -1) {
                    output.write(bytes,0,readCount);
                }
                output.flush();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (output != null) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }
        // 说明是文件夹,遍历这个文件夹
        File[] files = in.listFiles();
        for (File file: files) {
        	File newDir = new File(out.getAbsolutePath());
        	
            if (file.isDirectory()) {      //判断是否为文件夹,是
                // 新的目录为 out.getAbsolutePath() + "/" + in.getName()
                newDir = new File(out.getAbsolutePath()+"/"+file.getName());              
            }
            
            if (!newDir.exists()) {     //判断是否含有此文件夹,否
                newDir.mkdirs();        //创建子目录
            }
            System.out.println("in="+file.getAbsolutePath());
            System.out.println("out="+newDir.getAbsolutePath());
//             递归方法CopyDir
            CopyDir(file,newDir);
        }
    }
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧