1. public class Test {
  2. public static void main(String[] args){
  3. String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx";
  4. String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src";
  5. String outFileName = "2.docx";
  6. getFile(getBytes(filePath),outFilePath,outFileName);
  7. }
  8. // ----------------获得指定文件的byte数组 ----------------
  9. public static byte[] getBytes(String filePath){
  10. byte[] buffer = null;
  11. try {
  12. File file = new File(filePath);
  13. FileInputStream fis = new FileInputStream(file);
  14. ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
  15. byte[] b = new byte[1000];
  16. int n;
  17. while ((n = fis.read(b)) != -1) {
  18. bos.write(b, 0, n);
  19. }
  20. fis.close();
  21. bos.close();
  22. buffer = bos.toByteArray();
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. return buffer;
  29. }
  30. // ----------------根据byte数组,生成文件 ----------------
  31. public static void getFile(byte[] bfile, String filePath,String fileName) {
  32. BufferedOutputStream bos = null;
  33. FileOutputStream fos = null;
  34. File file = null;
  35. try {
  36. File dir = new File(filePath);
  37. if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
  38. dir.mkdirs();
  39. }
  40. file = new File(filePath+"\\"+fileName);
  41. fos = new FileOutputStream(file);
  42. bos = new BufferedOutputStream(fos);
  43. bos.write(bfile);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. } finally {
  47. if (bos != null) {
  48. try {
  49. bos.close();
  50. } catch (IOException e1) {
  51. e1.printStackTrace();
  52. }
  53. }
  54. if (fos != null) {
  55. try {
  56. fos.close();
  57. } catch (IOException e1) {
  58. e1.printStackTrace();
  59. }
  60. }
  61. }
  62. }
  63. }

java File和Byte[]数组 相互转换的更多相关文章

  1. JAVA中文件与Byte数组相互转换的方法

    JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...

  2. Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式)

    Redis入门 – Jedis存储Java对象 - (Java序列化为byte数组方式) 原文地址:http://alanland.iteye.com/admin/blogs/1600685(欢迎转载 ...

  3. Java 基础类型转换byte数组, byte数组转换基础类型

    Java 基础类型转换byte数组, byte数组转换基础类型 Java类型转换 java类对象转化为byte数组

  4. JAVA获取文件byte数组并输出进行展示和文件下载

    /** * 文件下载 */ @GetMapping(value = "/download") public void download(HttpServletResponse re ...

  5. Java 文件和byte数组转换

    /** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...

  6. Java 图片与byte数组互相转换

    //图片到byte数组 public byte[] image2byte(String path){ byte[] data = null; FileImageInputStream input = ...

  7. JAVA File转Byte[]

    /** * 获得指定文件的byte数组 */ public static byte[] getBytes(String filePath){ byte[] buffer = null; try { F ...

  8. 转转转--Java File和byte数据之间的转换

    package cn.iworker.file; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; ...

  9. Java官方操纵byte数组的方式

    java官方提供了一种操作字节数组的方法——内存流(字节数组流)ByteArrayInputStream.ByteArrayOutputStream ByteArrayOutputStream——by ...

随机推荐

  1. Python全栈工程师(数值类型、运算符)

    ParisGabriel       Python 入门基础     python的应用领域: 1.系统运维 2.网络编程(如:网络爬虫,搜索引擎,服务器编程) 3.科学计算 4.航空领域(如:卫星, ...

  2. Ubuntu16.04 问题汇总

    Ubuntu16.04安装wps并解决系统缺失字体问题 http://www.cnblogs.com/liutongqing/p/6388160.html

  3. SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XXX' (13)

    SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XXX' (13) 我可以真见识了 SELinux 的利害了, 这个问题让我找了好长时 ...

  4. python与MySQL数据库

    python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...

  5. jQuery Ajax(load,post,get,ajax)

    1.load(url, [data], [callback]) 载入远程 HTML 文件代码并插入至 DOM 中. 默认使用 GET 方式 - 传递附加参数时自动转换为 POST 方式.jQuery ...

  6. freemarker的简单入门程序

    本文主要介绍了freemarker的常用标签<#list>   <#import>  <#assign>  <#if> <#else> &l ...

  7. [ CodeVS冲杯之路 ] P1295

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1295/ 数据很小,直接DFS,加上剪枝 剪枝其实就是判重,首先深度是行下标,这里自带不重复,枚举的列下标,用 f 记录 ...

  8. 重建二叉树_C++

    一.题目背景 给定一个二叉树的前序和中序遍历,求出它的后序遍历 二叉树的遍历可参考 http://blog.csdn.net/fansongy/article/details/6798278/ 二.算 ...

  9. openGL深度缓冲区问题

    http://zhidao.baidu.com/question/368299839.html&__bd_tkn__=6aa9196c746cd3357f1eec74aeb127b395029 ...

  10. VC2010编译libwebsockets

    1. 安装cmake: https://cmake.org/files/v3.6/cmake-3.6.0-win64-x64.msi 2. 下载libwebsocket源码: git clone ht ...