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();
        }
        
    }
  • 相关阅读:
    两数之和
    IntelliJ IDEA为类和方法自动添加注释
    IDEA main 函数的快捷键
    Mac终端开启代理
    Pycharm节能模式
    使用正则表达式替换构造字典
    使用代理爬取微信文章
    利用 Scrapy 爬取知乎用户信息
    Scrapy选择器的用法
    Scrapy命令行基本用法
  • 原文地址:https://www.cnblogs.com/Iqiaoxun/p/5994585.html
Copyright © 2011-2022 走看看