zoukankan      html  css  js  c++  java
  • 文件和字节数组的相互转换

    package com.daxin.miaozhen;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * 
     * @author daxin
     * 
     * @email leodaxin@163com
     * 
     * @date 2017年9月14日 下午10:23:07
     * 
     */
    public class MainByte {
        public static void main(String[] args) throws Exception {
    
    
            byte[] bytes = file2Bytes("C:\Coding\miaozhen\src\com\daxin\miaozhen\StringDemo.java");
            byte2File(bytes);
            System.out.println(bytes.length);
    
        }
    
        public static byte[] file2Bytes(String path) throws Exception {
            File file = new File(path);
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
    
            byte[] data = bos.toByteArray();
            bos.close();
            bos = null;
            in.close();
            in = null;
            return data;
        }
    
        public static void byte2File(byte[] data) throws Exception {
            BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("StringDemo1.java.txt"));
            bo.write(data);
            bo.flush();
            bo.close();
        }
    
    }
  • 相关阅读:
    js字符串空格和换行
    python resources
    -eous
    英语资源网站
    -iatry 没病走两步
    book corpus
    epub converters
    brainstorm detain
    craftsman
    parachute
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7523634.html
Copyright © 2011-2022 走看看