1 /** 2 * 获得指定文件的byte数组 3 */ 4 5 private byte[] getBytes(String filePath){ 6 byte[] buffer = null; 7 try { 8 File file = new File(filePath); 9 FileInputStream fis = new FileInputStream(file); 10 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); 11 byte[] b = new byte[1000]; 12 int n; 13 while ((n = fis.read(b)) != -1) { 14 bos.write(b, 0, n); 15 } 16 fis.close(); 17 bos.close(); 18 buffer = bos.toByteArray(); 19 } catch (FileNotFoundException e) { 20 e.printStackTrace(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 return buffer; 25 }
1 /** 2 * 根据byte数组,生成文件 3 */ 4 public static void getFile(byte[] bfile, String filePath,String fileName) { 5 BufferedOutputStream bos = null; 6 FileOutputStream fos = null; 7 File file = null; 8 try { 9 File dir = new File(filePath); 10 if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在 11 dir.mkdirs(); 12 } 13 file = new File(filePath+"\\"+fileName); 14 fos = new FileOutputStream(file); 15 bos = new BufferedOutputStream(fos); 16 bos.write(bfile); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } finally { 20 if (bos != null) { 21 try { 22 bos.close(); 23 } catch (IOException e1) { 24 e1.printStackTrace(); 25 } 26 } 27 if (fos != null) { 28 try { 29 fos.close(); 30 } catch (IOException e1) { 31 e1.printStackTrace(); 32 } 33 } 34 } 35 }
时间: 2024-10-13 00:15:21