java文件转二进制

本工具类提供三个方法:

  • 1.文件转为二进制数组
  • 2.文件转为二进制字符串
  • 3.二进制字符串还原为文件
可当做工具类直接使用。
1package com.yscredit.sz.util; 2 3import org.springframework.util.FileCopyUtils; 4 5import java.io.ByteArrayOutputStream; 6import java.io.File; 7import java.io.FileInputStream; 8import java.io.InputStream; 9 10/** 11 * Author: momo 12 * Date: 2018/5/7 13 * Description:文件转为二进制 14 */ 15public class BinUtil { 16 17 18 19 20 public static void main(String[] args){ 21 File file = new File("E://ONO白皮书v2.0.pdf"); 22 String fileName = file.getName(); 23 binToFile(fileToBinStr(file),fileName,"E://测试byte"); 24 getFileToByte(file); 25 } 26 27 /** 28 * 文件转为二进制数组 29 * @param file 30 * @return 31 */ 32 public static byte[] fileToBinArray(File file){ 33 try { 34 InputStream fis = new FileInputStream(file); 35 byte[] bytes = FileCopyUtils.copyToByteArray(fis); 36 return bytes; 37 }catch (Exception ex){ 38 throw new RuntimeException("transform file into bin Array 出错",ex); 39 } 40 } 41 42 /** 43 * 文件转为二进制字符串 44 * @param file 45 * @return 46 */ 47 public static String fileToBinStr(File file){ 48 try { 49 InputStream fis = new FileInputStream(file); 50 byte[] bytes = FileCopyUtils.copyToByteArray(fis); 51 return new String(bytes,"ISO-8859-1"); 52 }catch (Exception ex){ 53 throw new RuntimeException("transform file into bin String 出错",ex); 54 } 55 } 56 57 58 /** 59 * 二进制字符串转文件 60 * @param bin 61 * @param fileName 62 * @param parentPath 63 * @return 64 */ 65 public static File binToFile(String bin,String fileName,String parentPath){ 66 try { 67 File fout = new File(parentPath,fileName); 68 fout.createNewFile(); 69 byte[] bytes1 = bin.getBytes("ISO-8859-1"); 70 FileCopyUtils.copy(bytes1,fout); 71 72 //FileOutputStream outs = new FileOutputStream(fout); 73 //outs.write(bytes1); 74 //outs.flush(); 75 //outs.close(); 76 77 return fout; 78 }catch (Exception ex){ 79 throw new RuntimeException("transform bin into File 出错",ex); 80 } 81 } 82 83 /** 84 * 文件转为二进制数组 85 * 等价于fileToBin 86 * @param file 87 * @return 88 */ 89 public static byte[] getFileToByte(File file) { 90 byte[] by = new byte[(int) file.length()]; 91 try { 92 InputStream is = new FileInputStream(file); 93 ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); 94 byte[] bb = new byte[2048]; 95 int ch; 96 ch = is.read(bb); 97 while (ch != -1) { 98 bytestream.write(bb, 0, ch); 99 ch = is.read(bb); 100 } 101 by = bytestream.toByteArray(); 102 } catch (Exception ex) { 103 throw new RuntimeException("transform file into bin Array 出错",ex); 104 } 105 return by; 106 } 107 108} 109

本文同步分享在 博客“IT云清”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringBoot自定义序列化的使用方式

场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x