package com.etc.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @deprecated 文件拷贝 前提是文件夹存在
* @author wcc
*
*/
public class CopyFile {
/**
* @param args
*/
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
File fromFile = new File("E:\\11.txt");
File toFile = new File("D:\\22.txt");
try {
copyFile.copyFile(fromFile, toFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public void copyFile(File fromFile, File toFile) throws Exception {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
if (fromFile.exists()) {
inputStream = new FileInputStream(fromFile);
outputStream = new FileOutputStream(toFile);
byte[] byte1 = new byte[1024];
int n = 0;
while ((n = inputStream.read(byte1)) != -1) {
outputStream.write(byte1, 0, n);
}
} else {
throw new Exception("文件不存在");
}
} catch (FileNotFoundException e) {
throw new FileNotFoundException("文件不存在" + e);
} catch (IOException e) {
throw new IOException("文件不存在" + e);
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
throw new IOException("关闭流错误" + e);
}
}
}
}