Java IO--字节流复制图片实例

时间:2021-10-25 21:35:01

字节流用来操作图片、视屏、音频(进制文件)

 1 package learn;
 2 
 3 import java.io.*;
 4 
 5 public class Learn{
 6     public static void main(String[] args) throws IOException {
 7         File file1=new File("D:/a.jpg");
 8         File file2=new File("D:/b.jpg");
 9         byte[] b=new byte[(int)file1.length()];
10         FileInputStream in=null;
11         FileOutputStream out=null;
12         try {
13             in=new FileInputStream(file1);
14             out=new FileOutputStream(file2);//没有指定文件则会创建
15             while(in.read(b)!=-1){        //read()--int,-1表示读取完毕
16                 out.write(b);
17             }
18             out.flush();
19             in.close();
20             out.close();
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         }
24     }
25 }