public static byte[] fileToBytes(String filePath) {
byte[] buffer = null;
File file = new File(filePath); FileInputStream fis = null;
ByteArrayOutputStream bos = null; try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
} buffer = bos.toByteArray();
} catch (FileNotFoundException ex) {
Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (null != bos) {
bos.close();
}
} catch (IOException ex) {
Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
} finally{
try {
if(null!=fis){
fis.close();
}
} catch (IOException ex) {
Logger.getLogger(JmsReceiver.class.getName()).log(Level.SEVERE, null, ex);
}
}
} return buffer;
}
public static void bytesToFile(byte[] buffer, final String filePath){

        File file = new File(filePath);

        OutputStream output = null;
BufferedOutputStream bufferedOutput = null; try {
output = new FileOutputStream(file); bufferedOutput = new BufferedOutputStream(output); bufferedOutput.write(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(null!=bufferedOutput){
try {
bufferedOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
} if(null != output){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

Java File 与 Bytes相互转换的更多相关文章

  1. Java File类总结和FileUtils类

    Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...

  2. java对象与xml相互转换 ---- xstream

    XStream是一个Java对象和XML相互转换的工具,很好很强大.提供了所有的基础类型.数组.集合等类型直接转换的支持. XStream中的核心类就是XStream类,一般来说,熟悉这个类基本就够用 ...

  3. Java File 类的使用方法详解

    Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对Java File文件操作类进行详细地分析,并将File类中的常用方法进行简单介绍,有需要的Java开发者可以看 ...

  4. Java File 类的使用方法详解(转)

    转自:http://www.codeceo.com/article/java-file-class.html Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对J ...

  5. WebService(2)-XML系列之Java和Xml之间相互转换

    源代码下载:链接:http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:使用jaxb完毕对象和xml之间的转换 TestJava2xml.java ...

  6. Java——File类成员方法

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  7. An error occurred at line: 1 in the generated java file问题处理

    tomcat6启动后,加载jsp页面报错,提示无法将jsp编译为class文件,主要报错信息如下: An error occurred at line: 1 in the generated java ...

  8. 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...

  9. Java File类 mkdir 不能创建多层目录

    File f = new File("/home/jp/Upload"); if ((!f.exists()) || (!f.isDirectory())) {boolean re ...

随机推荐

  1. 华为S5300系列交换机V100R005SPH020升级补丁

    S23_33_53-V100R005SPH020.pat 附件: 链接:https://pan.baidu.com/s/1-qgNEtRsZbNnC4eK4DTctA  密码:wpn3

  2. configure: error: lzo enabled but missing

    注意:yum安装的无效,需要手动下载源码安装. wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar zx ...

  3. js比较两个String字符串找出不同,并将不同处高亮显示

    根据java代码改写成js,下边js文件代码: function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.a ...

  4. 使用Lazy<T>实现对客户订单的延迟加载

    "延迟加载"是指在需要的时候再加载数据.比如获得一个Customer信息,并不会把该Customer的Orders信息一下加载出来,当需要显示Orders的时候再加载.简单来说,就 ...

  5. VirtualBox 安装虚拟机

  6. ConcurrentDictionary AddOrUpdate

    var sessionId = a.Session.SessionID.ToString(); userDic.AddOrUpdate( authUser.UserId, sessionId, (ke ...

  7. windows下androidNDK环境配置

    一:什么是NDK? NDK 提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so 和java 应用一起打包成apk.这些工具对开发者的帮助是巨大的. NDK 集成了交叉编译器, ...

  8. .NET:为什么需要逆变和协变

    为啥需要协变和逆变? 我目前想到的理由是:逆变和协变的目的是支持多态. 一个小例子 不明白为啥输出的是false和true. using System; using System.Collection ...

  9. Nginx+Memcached+Tomcat集群配置实践(Sticky Session)

    准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...

  10. TR069协议小结

        也称为CWMP,是在Internet网上通过wan口控制通信终端设备的协议.其协议流程如下图所示: 具体网上有很多资料.其主要的两个内容是:HTTP Client模型.DATA模型.      ...