zoukankan      html  css  js  c++  java
  • 字节流与文件流对接

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class ByteArrayTest2 {
        public static void main(String[] args) throws IOException {
            
            byte[] b = getBytesFromFile("test.txt");
            
            toFileFromBytes(b);
            
            String s = new String(b);
            
            System.out.println(s);
            
        }
        
        public static byte[] getBytesFromFile(String fileName) throws IOException{
            byte[] dest = null;
            
            InputStream is = new BufferedInputStream(new FileInputStream(fileName));
            
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            
            byte[] buff = new byte[1024];
            int len = 0;
            while((len=is.read(buff))!=-1){
                System.out.println(len);
                out.write(buff, 0, len);
            }
            
            dest = out.toByteArray();
            out.close();
            is.close();
            return dest;
        }
        
        
        public static void toFileFromBytes(byte[] b) throws IOException{
            InputStream is = new BufferedInputStream(new ByteArrayInputStream(b));
            
            OutputStream out = new BufferedOutputStream(new FileOutputStream("toFileFromBytes.txt"));
            
            byte[] buff = new byte[1024];
            int len = 0;
            while((len=is.read(buff))!=-1){
                System.out.println(len);
                out.write(buff, 0, len);
            }
            out.flush();
            out.close();
            is.close();
        }
        
    }
  • 相关阅读:
    python+requests+excel 接口测试
    Pycharm配置git
    ubuntu16.04+ROS安装kinectV1
    ubuntu16.04安装有道词典
    ROS kinetic语音识别
    在Ubuntu16.04中python环境下实现tab键补全
    ros kinetic安装rbx1
    ubuntu14.04安装opencv3.1
    ubuntu16.04SSH无法连接
    VC6中函数点go to definition报告the symbol XXX is undefined
  • 原文地址:https://www.cnblogs.com/Iqiaoxun/p/5994585.html
Copyright © 2011-2022 走看看