天天看点

把二进制文件转化为字节数组

看下面的静态方法:

把二进制文件转化为字节数组

/** 

     * 把二进制文件转化为字节数组 

     * @param path :path of specified file 

     * @return bytes[] 

     * @throws exception 

     */  

    public static byte[] tobytearrfromfile(string path) throws exception{  

        file infile = new file(path);  

        fileinputstream fileinputstream = new fileinputstream(infile);  

        bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();  

        int i;  

        //转化为字节数组流  

        while ((i = fileinputstream.read()) != -1) {  

            bytearrayoutputstream.write(i);  

        }  

        fileinputstream.close();  

        // 把文件存在一个字节数组中  

        byte[] bytes = bytearrayoutputstream.tobytearray();  

        bytearrayoutputstream.close();  

        return bytes;  

    }