51,408
社区成员
发帖
与我相关
我的任务
分享
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFile {
public void copy(String file1, String file2) throws Exception {
File in = new File(file1);
File out = new File(file2);
if (!in.exists()) {
throw new Exception("源文件不存在:" + in.getAbsolutePath());
}
if (!out.exists()) {
out.mkdirs();
}
FileInputStream fin = null;
FileOutputStream fout = null;
if(in.isFile()) {
fin = new FileInputStream(in);
fout = new FileOutputStream(new File(out + "/" + in.getName()));
int c;
byte[] b = new byte[1024 * 5];
while ((c = fin.read(b)) != -1) {
fout.write(b, 0, c);
}
fin.close();
fout.close();
} else {
System.out.println(in.getName() + "是文件,不能指定目录。");
}
}
public static void main(String[] args) throws Exception {
CopyFile copyFile = new CopyFile();
copyFile.copy("E:\\index\\segments.gen", "E:\\");
}
}