注:来源于JavaEye

文件转化为字节数组:

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

  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. }

Java中文件与字节数组转换的更多相关文章

  1. Java将文件转为字节数组

    Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...

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

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

  3. java 读取文件的字节数组

    /*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   St ...

  4. Java中如何将字符串数组转换成字符串

    如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...

  5. java中如何将字符串数组转换成字符串(转)

    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...

  6. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  7. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  8. java对获取的字节数组进行处理

    java对获取的字节数组bytes[]进行处理: 第一种,直接将该字节数组转换为字符串(部分): String content = ,); //从位置0开始获取2个字节 这样,对获取的数据报进行全部转 ...

  9. C#字节数组转换成字符串

    C#字节数组转换成字符串 如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了.为了进行这样的转换,我们不得不借助另一个类:System.Text.Enc ...

随机推荐

  1. https、socket、http协议

    一.https https 其实是由两部分组成:http+ssl(Secure Sockets Layer 安全套接层)/tls(Transport Layer Security 继任者安全传输层), ...

  2. HDU 6195 2017沈阳网络赛 公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6195 题意:有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个 ...

  3. Oracle常用sql语句(三)之子查询

    子查询 子查询要解决的问题,不能一步求解 分为: 单行子查询 多行子查询 语法: SELECT select_list FROM table WHERE expr operator (SELECT s ...

  4. 表格中上移下移置顶的js操作

    <script> $(function(){  //上移  var $up = $(".up")  $up.click(function() {   var $tr = ...

  5. 设计模式--工厂模式 caffe_layer注册

    来源:http://www.cnblogs.com/zhouqiang/archive/2012/07/20/2601365.html 来源:http://blog.luoyetx.com/2016/ ...

  6. oracle创建计划任务

    特别提示: oracle是执行完任务,才按照interval去计算下次执行时间!!! 为精确每个5分钟执行一个任务,必须自己计算时间. 如:trunc_minute(sysdate)+/ create ...

  7. Windows搭建python开发环境[一]

    首先需要去python的官网下载环境.鼠标移动到Downloads的tab上,在这里可以下载. python的环境还是很人性化的,没有那么多罗里吧嗦的配置什么的,下载好以后直接无脑next就行了,直到 ...

  8. PHP using mcrypt and store the encrypted in MySQL

    This is how I would do it. Create a class to do encryption/decryption: class cipher { private $secur ...

  9. TCP/IP——ARP与RARP简记

    ARP(Address Resolution Protocol):ARP为IP地址到对应的硬件地址(MAC)之间提供动态映射.这个过程是自动完成的,一般应用程序用户和系统管理员不必要关心. ARP高速 ...

  10. open -python操作文件

    一打开文件 二操作文件 三关闭文件 open(文件,模式,编码),打开文件----->0101010(以二进制的方式打开)------>编码(open默认utf-8编码)------> ...