【推荐阅读】微服务还能火多久?>>> 使用byteArrayInputStram-LMLPHP

ByteArrayInputStream 字节数组输入流(看作从内存把数据读到别的地方)

ByteArrayOutputStream 字节数组输出流,(把内存看作母的地,就很好理解)

public class IoTest1 {

public static void main(String[] args)  {
  byte [] s=f("a.jpg");
    System.out.println(s.length);
    f1(s,"d:/a_1.jpg");
}

public static byte[]  f(String src){
    File f=new File(src);
    FileInputStream in=null;
    try {
      in=new FileInputStream(f);
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      byte by[]=new byte[1024];
      int len=-1;
      while ((len=in.read(by))!=-1){
          baos.write(by,0,len);
      }
      return baos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(in!=null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static void f1(byte[] by,String name){
    InputStream is=null;
    is=new ByteArrayInputStream(by);
    File f = new File(name);
    FileOutputStream fs=null;
    try {
       fs=new FileOutputStream(f,true);
       byte[] t=new byte[1024];
       int len=-1;
       while ((len=is.read(t))!=-1){
           fs.write(t,0,len);
       }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fs!=null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

}

04-15 03:25