注:来源于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. }

最新文章

  1. .NET Core中ADO.NET SqlClient的使用与常见问题
  2. 深入浅出 Redis client/server交互流程
  3. CSS生僻问题一网打尽
  4. css2----兼容----ie67的3像素bug
  5. Projects\Portal_Content\Indexer\CiFiles文件夹下文件占用磁盘空间过大问题。
  6. EasyUi – 4.datagrid
  7. 【转】appStore上传苹果应用程序软件发布流程
  8. Java基础-String、StringBuffer、StringBuilder
  9. Servlet的生命周期及filter,servletRequest和servletResponse
  10. 深入理解javascript的闭包
  11. 修改 jquery easyui 表单验证默认的样式
  12. 第04讲- Android项目目录结构分析
  13. scp命令报错(IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!)
  14. vs 2015常用快捷键
  15. js表白心形特效
  16. 10分钟快速入门Redis
  17. java中带图片按钮的大小设置
  18. Android Studio 删除多余的虚拟设备(Virtual Device)
  19. 注意:Tomcat Get请求的坑!
  20. 基于OpenGL编写一个简易的2D渲染框架-03 渲染基本几何图形

热门文章

  1. 禁用quartz自动检查更新
  2. 在Mac上搭建Python虚拟环境
  3. Getting Started with Django Rest Framework and AngularJS
  4. u-boot界面添加命令[demo]
  5. AspNet Core 发布到Linux系统和发布IIS 注意项
  6. HDU 3001 Travelling(状态压缩DP+三进制)
  7. POJ 3616 Milking Time(最大递增子序列变形)
  8. csu 1553(RMQ+尺取法)
  9. Hive2.x 版本的安装及配置 以及要注意的事项
  10. java 用maven 构建项目时@Override错误的解决办法