注:来源于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. xshell连接Ubuntu虚拟机

    Ubuntu系统 1,安装ssh sudo apt-get install openssh-server 2,启动ssh进程 /etc/init.d/ssh start 3,查看进程信息 ps -e ...

  2. shell脚本自带变量的含义

    $0 Shell本身的文件名 $1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数… $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process ...

  3. linux系统定时任务设置

    .使用at命令设置一次性定时任务 2.使用crontab设置周期性定时任务 1)cd /home 目录下,使用vi test.py创建文件,内容如下: #!/usr/bin/python#coding ...

  4. sqlserver如何添加全文索引

    在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了.   下面简明的介绍如何使用Sql2008 全文索引 一.检查 ...

  5. 使用常见的网络命令查看当前网络状态——Mac OS X篇

    转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...

  6. python模块之xml.etree.ElementTree

    xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...

  7. vsftpd.log内容的意义

    vsftpd日志(xferlog格式)的含义 引用: Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a  ...

  8. python 面试

    知识总结 面试(一)

  9. DB2:FETCH FIRST 1 ROWS ONLY

    DB2:FETCH FIRST ROWS ONLY,在Oracle中使用where rownum=1代替 DB2: SELECT INSTORAGENO FROM ( SELECT max(AUDIT ...

  10. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8