一、使用FileInputStream和FileOutputStream读写非文本文件
/**
* 使用FileInputStream和FileOutputStream读写非文本文件
*/
public class test29 {
public static void main(String[] args){
//1、创建File类的对象,指明读入和写出的文件
File file = new File("流弊.jpg");
File file1 = new File("流弊2.jpg");
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
//2、创建输入流和输出流的对象
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
//3、复制的过程
byte[] buffer = new byte[5];
int len;
while((len=fileInputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、关闭流资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}