lz复制文件的时候,传递参数错误 CopyFile(src, file)。- 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 CopyDirectory {
- public static void main(String[] args) throws IOException {
- File src = new File("d:\\src");
- File target = new File("e:\\");
- CopyDir(src, target);
- }
- // 复制目录
- public static void CopyDir(File src, File target) throws IOException {
- if (src.isDirectory()) {
- File dir = new File(target, src.getName());
- dir.mkdirs();
- File[] filesrc = src.listFiles();
- for (File file : filesrc) {
- CopyDir(file, dir);
- }
- } else {
- File file = new File(target, src.getName());
- file.createNewFile();
- CopyFile(src, file);//这里面按照private static void CopyFile(File src, File target)应该是这样。
- }
- }
- // 复制文件
- private static void CopyFile(File src, File target) throws IOException {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- src));
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(target));
- byte[] bys = new byte[1024];
- int len = 0;
- while ((len = bis.read(bys)) != -1) {
- bos.write(bys);
- bos.flush();
- }
- bos.close();
- bis.close();
- }
- }
复制代码 |