输入输出流-通过字节数组保存

  1 package demo;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.DataInputStream;
  5 import java.io.DataOutputStream;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 import java.io.OutputStream;
 11 import java.util.ArrayList;
 12 
 13 //复制照片要求,把照片用字节数组存到文件中,再从文件中读数据写出来
 14 public class Test7 {
 15     //把照片数据写入文件中
 16     public static void writePicture(byte[] b,String path) {
 17         OutputStream os=null;
 18         
 19         try {
 20             os=new FileOutputStream(path);
 21             os.write(b);
 22         } catch (FileNotFoundException e) {
 23             e.printStackTrace();
 24         } catch (IOException e) {
 25             e.printStackTrace();
 26         }finally {
 27             try {
 28                 if(os!=null) {
 29                     os.close();
 30                 }
 31                 
 32             } catch (IOException e) {
 33                 e.printStackTrace();
 34             }
 35         }
 36     }
 37     
 38     //从指定文件中读取照片数据
 39     public static byte[] readPicture(String path) {
 40         FileInputStream fis=null;
 41         DataInputStream dis=null;
 42         try {
 43             fis=new FileInputStream(path);
 44             dis=new DataInputStream(fis);
 45             byte[] b=new byte[dis.available()];
 46             dis.read(b);
 47             return b;
 48         } catch (FileNotFoundException e) {
 49             e.printStackTrace();
 50         } catch (IOException e) {
 51             e.printStackTrace();
 52         }finally {
 53             try {
 54                 if(dis!=null) {
 55                     dis.close();
 56                 }
 57                 if(fis!=null) {
 58                     fis.close();
 59                 }
 60                 
 61             } catch (IOException e) {
 62                 e.printStackTrace();
 63             }
 64             
 65         }
 66         return null;
 67     }
 68     
 69     //从字节数组里读取内容写到指定文件中
 70     public static void writeByte(String path,byte[] b) {
 71         FileOutputStream fos=null;
 72         DataOutputStream dos=null;
 73         
 74         try {
 75             fos=new FileOutputStream(path);
 76             dos=new DataOutputStream(fos);  
 77             dos.write(b);  //把数据写入二进制文件
 78         } catch (FileNotFoundException e) {
 79             e.printStackTrace();
 80         } catch (IOException e) {
 81             e.printStackTrace();
 82         }finally {
 83             try {
 84                 if(dos!=null) {
 85                     dos.close();
 86                 }
 87                 if(fos!=null) {
 88                     fos.close();
 89                 }
 90                 
 91             } catch (IOException e) {
 92                 e.printStackTrace();
 93             }
 94             
 95         }
 96     }
 97     
 98     //用字节流读取照片数据到字节数组中
 99     public static byte[] getPictureByte(String Path) {
100         FileInputStream fis=null;
101         ByteArrayOutputStream baos=new ByteArrayOutputStream();
102         try {
103             fis=new FileInputStream(Path);
104             byte[] b=new byte[1024];
105             int len=-1;
106             while((len=fis.read(b))!=-1) {
107                 baos.write(b, 0, len);
108             }
109             return baos.toByteArray();
110         } catch (FileNotFoundException e) {
111             e.printStackTrace();
112         } catch (IOException e) {
113             e.printStackTrace();
114         }finally {
115             try {
116                 if(fis!=null) {
117                     fis.close();
118                 }
119             } catch (IOException e) {
120                 e.printStackTrace();
121             }
122             
123         }
124         return null;
125         
126     }
127     
128     public static void main(String[] args) {
129         byte[] picture=getPictureByte("d:\\2.JPG");
130         System.out.println(picture.length);
131         writeByte("test.txt",picture);
132         byte[] outImg = readPicture("test.txt");
133         System.out.println(outImg.length);
134         
135         writePicture(outImg,"2.jpg");
136     }
137 }

猜你喜欢

转载自www.cnblogs.com/baichang/p/10205668.html
今日推荐