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();
        }
        
    }
  • 相关阅读:
    jquery编写插件的方法
    $.extend()的深拷贝和浅拷贝详细讲解
    jquery实现表格的搜索功能
    网页中tab标签切换分别用jquery和javascript源码实现
    JavaScript和jQuery的类型判断
    chrome jssip
    APiCloud学习
    vue生命周期、钩子函数
    闭包、作用域、原型小知识点等面试小知识点
    前后端分离
  • 原文地址:https://www.cnblogs.com/Iqiaoxun/p/5994585.html
Copyright © 2011-2022 走看看