java 文件夹拷贝

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
public boolean copy(String file1, String file2) {
// 生成路径
File in = new File(file1);
File out = new File(file2);
// 验证是否存在现有路径
if (!in.exists()) {
System.out.println(in.getAbsolutePath() + "源文件路径错误!!!");
return false;
} else {
System.out.println("源文件路径" + in.getAbsolutePath());
System.out.println("目标路径" + out.getAbsolutePath());
}
if (!out.exists())
out.mkdirs();

File[] file = in.listFiles();
FileInputStream fin = null;
FileOutputStream fout = null;

for (int i = 0; i < file.length; i++) {
if (file[i].isFile()){
// 读取文件流
try{
fin = new FileInputStream(file[i]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("in.name=" + file[i].getName());
// 生成文件流路径及文件名
try {
fout = new FileOutputStream(new File(file2 + "/"
+ file[i].getName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(file2);

int c;
byte[] b = new byte[1024 * 5];
try {
while ((c = fin.read(b)) != -1) {

fout.write(b, 0, c);
System.out.println("复制文件中!");
}
fin.close();
fout.flush();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
// return true;
} else
// 递归应用
copy(file1 + "/" + file[i].getName(), file2 + "/"
+ file[i].getName());
}
return false;
}

public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("D:\\学习资料\\其他文档\\a_文档资料", "D:\\Copy");
}
}

你可能感兴趣的:(java,C++,c,C#)