文件与二进制数据互转-ByteArrayOutputStream
来源:互联网 发布:linux shell启动jar包 编辑:程序博客网 时间:2024/05/16 05:02
// 获取二进制数据public static byte[] getFileBinary(String filePath) { FileInputStream fis = null; BufferedInputStream bis = null; ByteArrayOutputStream baos = null; try { fis = new FileInputStream(filePath); bis = new BufferedInputStream(fis); baos = new ByteArrayOutputStream(); int c = bis.read(); while (c != -1) { // 数据存储到ByteArrayOutputStream中 baos.write(c); c = bis.read(); } fis.close(); bis.close(); // 转换成二进制 return baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { // 没有关闭ByteArrayOutputStream流的意义,空实现 try { if (fis != null ) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null ) { bis.close(); } } catch (IOException e) { e.printStackTrace(); } } } return null;}// 二进制数据转成文件public static void binaryToFile(byte[] bytes, String filePath) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { fos = new FileOutputStream(filePath); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null ) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bos != null ) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
ByteArrayOutputStream没有执行close()的意义,原因:底层空实现(源码如下)
0 0
- 文件与二进制数据互转-ByteArrayOutputStream
- java使用ByteArrayOutputStream读取任意大小的二进制数据
- 使用ByteArrayOutputStream下载文件
- C#实现文件与二进制互转并存入数据库
- 用JAVA处理文本与二进制数据混合大文件
- ByteArrayInputStream与ByteArrayOutputStream类
- ByteArrayOutputStream 与 ObjectOutputStream 转换
- ByteArrayInputStream与ByteArrayOutputStream
- java.ByteArrayInputStream与ByteArrayOutputStream
- ByteArrayOutputStream与InputStream
- ByteArrayInputStream与ByteArrayOutputStream详解
- ByteArrayOutputstream与FileOutputstream
- ByteArrayinputStream与ByteArrayOutputStream,printStream
- 文件与二进制流互转
- 文件与二进制流互转
- 文本数据与二进制数据
- ByteArrayOutputStream 大文件内存溢出
- InputStream转ByteArrayOutputStream
- 解决Android SDK Manager 更新、下载慢以及待安装包列表不显示的问题
- EasyUI弹出框,随滚动条移动 弹出图片预览自动缩略
- python的logging
- Android学习笔记__2__Android工程目录结构
- org.apache.log4j.Logger 详解
- 文件与二进制数据互转-ByteArrayOutputStream
- 王立平--eclipse中Ctrl+Shift+F格式化代码快捷键不管用了?
- 构建struts2框架的基本方法
- 定制Linux发行版:向CentOS6安装镜像添加自定义软件包
- 实现自己的LUA绑定器-一个模板编程挑战
- Python stat 模块
- Android学习笔记__3__Android应用程序组成
- 高手速成android开源项目【View篇】
- DataGrid控件的基本使用方法