1 package cn.zhang.io; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 13 /** 14 * 文件讀取到字節數組,再從字節數組讀取到文件 15 * 四個步驟 16 * 1、創建源 17 * 2、選擇流 18 * 3、操作 19 * 4、釋放資源 20 * @author zhang 21 * 22 */ 23 public class IOTest10 { 24 public static void main(String[] args) { 25 //指定文件源,獲得該文件的字節數組 26 byte[] datas = fileToByteArray("very_nice_satellite_images_of_iran-wallpaper-2880x1620.jpg");//圖片轉為字節數組 27 byteArrayToFile(datas,"picture.jpg");//字節數組轉為圖片 28 } 29 30 /** 31 * 1、文件讀取到字節數組,以程序為中心 32 * 1)、文件到程序: 33 * 源:main方法中 34 * 流:InputStream、FileInputStream 35 * 操作:輸入 36 * 釋放資源:需要釋放 37 * 程序到字節數組: 38 * 源:內部維護,自動創建一個字節數組 39 * 流:ByteArrayOutputStream,不能使用多態,使用ByteArrayOutputStream流中的toByteArray方法,返回字節數組 40 * 操作:輸出 41 * 釋放資源:不需要釋放 42 * 2)、結果是獲得一個字節數組 43 * @param filePath 44 * @return 45 */ 46 public static byte[] fileToByteArray(String filePath) { 47 //創建源與目的地 48 File src = new File(filePath);//獲得文件的源頭,從哪開始傳入(源) 49 byte[] dest = null;//最后在內存中形成的字節數組(目的地) 50 //選擇流 51 InputStream is = null;//此流是文件到程序的輸入流 52 ByteArrayOutputStream baos= null;//此流是程序到新文件的輸出流 53 //操作(輸入操作) 54 try { 55 is = new FileInputStream(src);//文件輸入流 56 baos = new ByteArrayOutputStream();//字節輸出流,不需要指定文件,內存中存在 57 byte[] flush = new byte[1024*10];//設置緩沖,這樣便於傳輸,大大提高傳輸效率 58 int len = -1;//設置每次傳輸的個數,若沒有緩沖的數據大,則返回剩下的數據,沒有數據返回-1 59 while((len = is.read(flush)) != -1) { 60 baos.write(flush,0,len);//每次讀取len長度數據后,將其寫出 61 } 62 baos.flush();//刷新管道數據 63 dest = baos.toByteArray();//最終獲得的字節數組 64 return dest;//返回baos在內存中所形成的字節數組 65 } catch (FileNotFoundException e) { 66 e.printStackTrace(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 }finally { 70 //釋放資源,文件需要關閉,字節數組流無需關閉 71 if(null != is) { 72 try { 73 is.close(); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 } 78 79 } 80 return null; 81 } 82 83 /** 84 * 字節數組寫出到新的文件 85 *1、字節數組讀入程序中 86 * 源:傳入的字節數組 87 * 流:ByteInputStream 88 * 操作:讀入 89 * 釋放資源:不需要 90 *2、程序寫出到新的文件中 91 * 源:傳入的新文件目的地 92 * 流:OutputStream FileOutputStream 93 * 操作:寫出 94 * 釋放資源:需要 95 * @param src 96 * @param filePath 97 */ 98 public static void byteArrayToFile(byte[] src,String filePath) { 99 //創建源 100 File dest = new File(filePath);//目的地,新文件 101 //src字節數組已經存在 102 //選擇流 103 InputStream is = null;//ByteArrayInputStream的父類 104 OutputStream os = null; 105 //操作 106 try { 107 is = new ByteArrayInputStream(src);//字節數組與程序之間的管道 108 os = new FileOutputStream(dest);//程序與新文件之間的管道 109 //一樣的字節數組緩沖操作 110 byte[] flush = new byte[1024*10]; 111 int len = -1; 112 while((len = is.read(flush)) != -1) { 113 os.write(flush,0,len); 114 } 115 os.flush(); 116 } catch (FileNotFoundException e) { 117 e.printStackTrace(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 }finally { 121 if(null != os) {//關閉文件流 122 try { 123 os.close(); 124 } catch (IOException e) { 125 e.printStackTrace(); 126 } 127 } 128 } 129 } 130 }