Java File ,byte[] , 二进制字符串之间互转

File ,byte[] , 二进制字符串之间互转的多种方法汇集

    /**
     * File转Byte[] 方法一
     * @param file
     * @return
     */
    public static byte[] fileToBinArray(File file){
        try {
            InputStream fis = new FileInputStream(file);
            byte[] bytes = FileCopyUtils.copyToByteArray(fis);
            return bytes;
        }catch (Exception ex){
            throw new RuntimeException("transform file into bin Array 出错",ex);
        }
    }

    /**
     * File转Byte[] 方法二
     * @param file
     * @return
     */
    public static byte[] getFileToByte(File file) {
        byte[] by = new byte[(int) file.length()];
        try {
            InputStream is = new FileInputStream(file);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            throw new RuntimeException("transform file into bin Array 出错",ex);
        }
        return by;
    }


    /**-
     * byte[]转File方法一
     * @param bytes
     * @param fileName
     * @return
     */
    public static File getFileByBytes(byte[] bytes,String fileName) {

        File file = new File("Tempalte",fileName);

        try {
            FileUtils.writeByteArrayToFile(file, bytes);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }

    /**
     * byte[]转File方法二
     * @param b
     * @param outputFileName
     * @return
     */
    public static File getFileFromBytes(byte[] b, String outputFileName){
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File("Tempalte",outputFileName);
            FileOutputStream fileStream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fileStream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }


    /**
     * File转为二进制字符串
     * @param file
     * @return
     */
    public static String fileToBinStr(File file){
        try {
            InputStream fis = new FileInputStream(file);
            byte[] bytes = FileCopyUtils.copyToByteArray(fis);
            return new String(bytes,"ISO-8859-1");
        }catch (Exception ex){
            throw new RuntimeException("transform file into bin String 出错",ex);
        }
    }

   /**
     * 二进制字符串转File
     * @param bin
     * @param fileName
     * @return
     */
    public static File binToFile(String bin,String fileName){
        try {
            File fout = new File("TempFile",fileName);
            fout.createNewFile();
            byte[] bytes1 = bin.getBytes("ISO-8859-1");
            FileCopyUtils.copy(bytes1,fout);
            return fout;
        }catch (Exception ex){
            throw new RuntimeException("transform bin into File 出错",ex);
        }
    }
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页