将文件转换成byte数组

工具类:

/**
     * 将文件转换成byte数组
     * @param file
     * @return
     */
    public static byte[] File2byte(File file){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }

版权声明:本文为qq_26169011原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。