package io;
import java.io.*;
/**
* @author 苗晓强
* @date 2023/8/5 17:16
* FileInputStream/FileOutputStream 使用字节流处理非文本文件
*/
public class FileStreamTest {
public static void main(String[] args) {
File srcFile = new File("picture.jpg");
File destFile = new File("picture_copy.jpg");
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFile);
fileOutputStream = new FileOutputStream(destFile);
int len;
byte [] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
System.out.println("复制成功!");
} 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();
}
}
}
}
}
IO流-复制非文本文件
最新推荐文章于 2024-01-08 22:26:03 发布