字节流--读取并复制图片

时间:2022-01-25 20:59:48
/**
* 字节流--读取并复制图片
* @param targetPath
*/
public static void readImageAndCopy(String targetPath){
File file=new File("F:\\CreateDemo\\demo.jpg");
FileInputStream fileInputStream=null;
try{
//输入流-读取文件
fileInputStream=new FileInputStream(file);
BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);

//输出流-写入文件
FileOutputStream fileOutputStream=new FileOutputStream(targetPath);
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);

byte[] b=new byte[1024];
int i=0;
while ((i=bufferedInputStream.read(b))!=-1){
bufferedOutputStream.write(b,0,i);
System.out.println(i);
}
bufferedOutputStream.close();
fileOutputStream.close();
bufferedInputStream.close();
fileInputStream.close();
}catch (FileNotFoundException e){
System.out.println("文件不存在");
e.printStackTrace();
}catch (IOException e){
System.out.println("读取文件错误");
e.printStackTrace();
}
}