• Java中文件与字节数组转换


    注:来源于JavaEye

    文件转化为字节数组:

    http://www.javaeye.com/topic/304980

    [c-sharp] view plaincopy
    1. /** 
    2.      * 文件转化为字节数组 
    3.      *  
    4.      * @param file 
    5.      * @return 
    6.      */  
    7.     public static byte[] getBytesFromFile(File file) {  
    8.         byte[] ret = null;  
    9.         try {  
    10.             if (file == null) {  
    11.                 // log.error("helper:the file is null!");  
    12.                 return null;  
    13.             }  
    14.             FileInputStream in = new FileInputStream(file);  
    15.             ByteArrayOutputStream out = new ByteArrayOutputStream(4096);  
    16.             byte[] b = new byte[4096];  
    17.             int n;  
    18.             while ((n = in.read(b)) != -1) {  
    19.                 out.write(b, 0, n);  
    20.             }  
    21.             in.close();  
    22.             out.close();  
    23.             ret = out.toByteArray();  
    24.         } catch (IOException e) {  
    25.             // log.error("helper:get bytes from file process error!");  
    26.             e.printStackTrace();  
    27.         }  
    28.         return ret;  
    29.     }  

    字节数组转化为文件

    http://www.javaeye.com/topic/304982

    1. /** 
    2.      * 把字节数组保存为一个文件 
    3.      *  
    4.      * @param b 
    5.      * @param outputFile 
    6.      * @return 
    7.      */  
    8.     public static File getFileFromBytes(byte[] b, String outputFile) {  
    9.         File ret = null;  
    10.         BufferedOutputStream stream = null;  
    11.         try {  
    12.             ret = new File(outputFile);  
    13.             FileOutputStream fstream = new FileOutputStream(ret);  
    14.             stream = new BufferedOutputStream(fstream);  
    15.             stream.write(b);  
    16.         } catch (Exception e) {  
    17.             // log.error("helper:get file from byte process error!");  
    18.             e.printStackTrace();  
    19.         } finally {  
    20.             if (stream != null) {  
    21.                 try {  
    22.                     stream.close();  
    23.                 } catch (IOException e) {  
    24.                     // log.error("helper:get file from byte process error!");  
    25.                     e.printStackTrace();  
    26.                 }  
    27.             }  
    28.         }  
    29.         return ret;  
    30.     } 
  • 相关阅读:
    【每日一具3】推荐一个4K、蓝光、3D高清影视下载站,影视资源丰富 发烧友必备
    Python对程序中异常进行处理
    通过一个简单的例子,了解 Cypress 的运行原理
    ABAP 标准培训教程 BC400 学习教程之一:ABAP 服务器的架构和一个典型的 ABAP 程序结构介绍
    如何安装最新版本的 SAP ABAP Development Tool ( ADT ) 2021年度更新
    ABAP R3 时代著名的 SFLIGHT 航班模型测试数据,到了S/4HANA时代的进化版
    SAP Fiori Elements 应用的 i18n 语法使用方式
    SAP Fiori Elements List Report 里的表格类型(tableType)是如何决定出来的
    使用 XSLT 给 SAP PI 增加 CDATA
    SAP Fiori Elements 学习笔记
  • 原文地址:https://www.cnblogs.com/langtianya/p/3968972.html
Copyright © 2020-2023  润新知