Java 文件和byte数组转换
来源:互联网 发布:八匹马网络加速器 编辑:程序博客网 时间:2024/06/16 05:47
直接上代码
/** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 根据byte数组,生成文件 */ public static void getFile(byte[] bfile, String filePath,String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在 dir.mkdirs(); } file = new File(filePath+"\\"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }
0 0
- java 文件和byte数组相互转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- Java 文件和byte数组转换
- java :文件和byte数组转换
- 文件和byte数组之间相互转换
- java int和byte数组的转换
- java int 和 byte数组转换
- java ByteBuffer和byte 数组相互转换
- java File和Byte[]数组 相互转换
- 文件和byte数组
- 使用JAVA将File文件转换为byte[]数组
- java 文件操作之byte数组间的转换
- java 实现 图片文件 与 byte 数组互相转换
- 图片文件和byte字节数组的转换
- c 语言的错误处理
- Codeforces 590 A:Median Smoothing
- CentOS6.x ffmpeg编译出现的问题
- 如何更换User Profile Synchronization Service Account?
- Android项目 集成 支付宝功能
- Java 文件和byte数组转换
- Spring中配置数据源的4种形式
- 设计模式-模板方法
- mvvm中各标签解释
- android之横向的listview
- Nexus9激活:卡在“正在检查网络连接” 最简单解决办法
- iOS 网络编程模式总结
- Android项目,从web上取下汉字,中文部分乱码
- 对与locale和resourceBundle的理解