import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public long copyFile1(File srcFile, File destDir, String newFileName) {
long copySizes = 0;
if (!srcFile.exists()) {
System.out.println("源文件不存在");
copySizes = -1;
} else if (!destDir.exists()) {
System.out.println("目标目录不存在");
copySizes = -1;
} else if (newFileName == null) {
System.out.println("文件名为null");
copySizes = -1;
} else {
try {
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(new File(destDir, newFileName)));
int b = 0, i = 0;
while ((b = bin.read()) != -1) {
bout.write(b);
i++;
}
bout.flush();
bin.close();
bout.close();
copySizes = i;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return copySizes;
}
/**
* 测试文件复制
* @param arg
*/
public static void main(String[] arg){
File ywj=new File("D://index.html");
File mbwj=new File("D://new");
new Copy().copyFile1(ywj,mbwj , "text.html");
}
}
java 文件复制
最新推荐文章于 2024-07-24 19:35:58 发布