转转转--Java File和byte数据之间的转换
package cn.iworker.file; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileTest { public static void main(String[] args) { file2BetyArray(); fileToBetyArray(); BetyToFile("D:\\Study\\Java\\First.class"); } public static byte[] file2BetyArray() { File file = new File("D:\\Study\\Java\\First.class"); if (!file.exists()) { return null; } FileInputStream stream = null; ByteArrayOutputStream out = null; try { stream = new FileInputStream(file); out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = stream.read(b)) != -1) { out.write(b, 0, n); } return out.toByteArray();// 此方法大文件OutOfMemory } catch (Exception e) { System.out.println(e.toString()); } finally { try { stream.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static byte[] fileToBetyArray() { FileInputStream fileInputStream = null; File file = new File("D:\\Study\\Java\\First.class"); byte[] bFile = null; try { bFile = new byte[(int) file.length()]; fileInputStream = new FileInputStream(file); fileInputStream.read(bFile); fileInputStream.close(); for (int i = 0; i < bFile.length; i++) { System.out.print((char) bFile[i]); } System.out.println("Done"); } catch (Exception e) { e.printStackTrace(); } finally { try { fileInputStream.close(); bFile.clone(); } catch (IOException e) { e.printStackTrace(); } } return bFile; } public static File BetyToFile( String filePath) { File file = new File(filePath); BufferedOutputStream stream = null; FileOutputStream fstream = null; byte[] data=new byte[(int)file.length()]; try { fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(data); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stream != null) { stream.close(); } if (null != fstream) { fstream.close(); } } catch (IOException e1) { e1.printStackTrace(); } } return file; } }
public static void byte2File(byte[] buf, 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 + File.separator + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(buf); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }