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

Posted Hepburn Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        
    
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于Java File ,byte[] , 二进制字符串之间互转的主要内容,如果未能解决你的问题,请参考以下文章

php中图片以字节流的形式直接发送七牛

位(bit)字节(byte)字

十六进制转汉字

位,字节和字区别(bit,byte,word)

IT十八掌作业_java基础第十四天_IO

java IO 学习