java File和Byte[]数组 相互转换
来源:互联网 发布:常见20网络诈骗 编辑:程序博客网 时间:2024/06/07 00:51
public class Test { public static void main(String[] args){ String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx"; String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src"; String outFileName = "2.docx"; getFile(getBytes(filePath),outFilePath,outFileName); } //获得指定文件的byte数组 public static 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 File和Byte[]数组 相互转换
- java 文件和byte数组相互转换
- java ByteBuffer和byte 数组相互转换
- Java中字符和byte数组之间的相互转换
- java中的int类型和byte数组的相互转换
- Java中字符和byte数组之间的相互转换
- golang和java的byte数组相互转换
- Java中字符和byte数组之间的相互转换
- Java byte数组和文件相互转换
- c# string和byte数组相互转换
- byte数组和结构体相互转换
- int和byte数组相互转换
- bitmap和byte数组的相互转换
- 文件和byte数组之间相互转换
- java中byte数组和String字符串怎么相互转换?(string 转 byte[]和byte[] 转 string)
- java中 int和byte相互转换
- java 中 image 和 byte[] 相互转换
- java 中 image 和 byte[] 相互转换
- Python3的语法学习
- vim的powerline插件没有颜色
- 2016个推详细配置详解
- Python-strip(),lstrip(),rstrip()
- 多线程下载
- java File和Byte[]数组 相互转换
- python scp 批量同步文件
- g09莫名崩溃原因
- Android Volley完全解析
- HDU-4081-Qin Shi Huang's National Road System
- 加密解密实现和原理
- 设计模式总结之策略模式
- hdu5573 Binary Tree 二进制
- C【语言】计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。