天天看點

把二進制檔案轉化為位元組數組

看下面的靜态方法:

把二進制檔案轉化為位元組數組

/** 

     * 把二進制檔案轉化為位元組數組 

     * @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;  

    }