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. DOM-XML(解析与创建)

    /** * 读取(解析)xml文件 * @author Administrator * */ public class DOMRead { public static void main(String ...

  2. Go 收藏

    Golang 定位解决分布式系统,服务器应用开发,主要竞争对手是 Java.Python 之类:Rust 定位解决单机安全问题,高性能场景偏系统底层开发,主要竞争对手就是 C 和 C++. Golan ...

  3. mysql安装三 linux源码安装mysql5.6.22

    http://blog.csdn.net/beiigang/article/details/43053803

  4. python定位性能的工具

    参考: http://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/ http://xianglong.me/article/analys ...

  5. Matlab的linprog解决简单线性规划问题

    一个简单的线性规划问题,使用Matlab的linprog解决 假定有n种煤,各种煤的配比为x1,x2,x3,……首先需要满足下列两个约束条件,即 x1+x2+x3……+xn=1 x1≥0, x2≥0, ...

  6. C#Arcengine通过坐标点生成面(环形)

    来自:http://www.cnblogs.com/lee24789229/p/5481978.html 通过传入坐标点,返回几何图形,此代码部分可以生成环形面. 方法一 private IGeome ...

  7. Spring3数据库事务管理机制

    Spring对事务的解决办法其实分为2种:编程式实现事务,AOP配置声明式解决方案. http://jinnianshilongnian.iteye.com/blog/1496953 Spring提供 ...

  8. 通过@Value注解读取.properties配置内容

    @Controller @RequestMapping("/value") public class ValuePropertyController extends Applica ...

  9. dwz ajax session超时跳转登录页(struts2自定义拦截器)

    1.定义struts2拦截器(网上例子很多) 代码如下: package rt.intercepter; import java.util.Map; import javax.servlet.http ...

  10. java执行ping命令

    public static void get() throws IOException{ String address="10.132.118.110"; Process proc ...