package video;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestCopy {
public static void main(String[] args) {
RunIO();
}
public static void RunIO() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File file1 = new File("C:\\Users\\22847\\Desktop\\图片素材ds\\ERSP.jpg");
File file2 = new File("C:\\Users\\22847\\Desktop\\ERSP.jpg");
fileInputStream = new FileInputStream(file1);
fileOutputStream = new FileOutputStream(file2);
int length = 0;
byte[] buf = new byte[1024];
while ((length = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, length);
}
}catch (IOException e){
throw new RuntimeException(e);
}finally {
try {
if (fileOutputStream !=null)
{
fileOutputStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fileInputStream!=null)
{
fileInputStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1:为了方便函数的调用在抛出异常的时候选择新建包装类 RnutimeException并传入异常。
2:一定要让关闭文件的语句有机会执行。