文件拷贝

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

public class Test_05 {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("test_01");
fileOutputStream = new FileOutputStream("test_02");
byte[] bytes = new byte[2];
int readCount = 0;
while ((readCount = fileInputStream.read(bytes)) != -1){
fileOutputStream.write(bytes, 0, readCount);
}
fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

*注意:当创建了多个流时,finall语句块中try...catch要分开写,一个流的关闭发生异常时不能影响其他流的关闭
posted @ 2020-08-09 11:01  javase-->  阅读(85)  评论(0)    收藏  举报