首页 > 编程 > Java > 正文

Java复制多级文件夹

2019-11-06 07:05:58
字体:
来源:转载
供稿:网友
package cn.itcast_02;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class CopyFileDemo { public static void main(String[] args) throws IOException { File scrFile = new File("e://demo"); File destFile = new File("d://"); copyFile(scrFile, destFile); } PRivate static void copyFile(File scrFile, File destFile) throws IOException { if (scrFile.isDirectory()) { File newFolder = new File(destFile, scrFile.getName()); newFolder.mkdir(); File[] fileArray = scrFile.listFiles(); for (File file : fileArray) { copyFile(file, newFolder); } } else { File newFile = new File(destFile, scrFile.getName()); copy(scrFile, newFile); } } private static void copy(File scrFile, File destFile) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream( scrFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表