標籤:port 檔案的 get 位元組流 man print 檔案 傳遞 close
輸出位元組流:
--------| OutputStream 是所有輸出位元組流 的父類。 抽象類別
-----------| FileOutStream 向檔案輸出資料的輸出位元組流。
FileOutputStream如何使用呢?
1. 找到目標檔案
2. 建立資料的輸出通道。
3. 把資料轉換成位元組數組寫出。
4. 關閉資源
方法一:
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class outstream { public static void main(String[] args) throws IOException { writetest(); System.out.println("寫入完成了,,,,,,"); } public static void writetest() throws IOException{ File file=new File("c:\\a.txt"); FileOutputStream fileOutputStream=new FileOutputStream(file); fileOutputStream.write(‘h‘); fileOutputStream.write(‘e‘); fileOutputStream.write(‘l‘); fileOutputStream.write(‘l‘); fileOutputStream.write(‘o‘); fileOutputStream.close(); }}
此時將hello寫入a.txt檔案中。
每次只能將一個字元寫入,比較麻煩。
方法二:
//使用位元組數組把資料寫出。 public static void writeTest2() throws IOException{ //找到目標檔案 File file = new File("F:\\b.txt"); //建立資料輸出通道 FileOutputStream fileOutputStream = new FileOutputStream(file,true); //把資料寫出。 String data = "\r\nhello world"; fileOutputStream.write(data.getBytes()); //關閉資源 fileOutputStream.close(); }
FileOutputStream要注意的細節:
1. 使用FileOutputStream 的時候,如果目標檔案不存在,那麼會自動建立目標檔案對象。
2. 使用FileOutputStream寫資料的時候,如果目標檔案已經存在,那麼會先清空目標檔案中的資料,然後再寫入資料。
3.使用FileOutputStream寫資料的時候, 如果目標檔案已經存在,需要在原來資料基礎上追加資料的時候應該使用new FileOutputStream(file,true)建構函式,第二參數為true。
4.使用FileOutputStream的write方法寫資料的時候,雖然接收的是一個int類型的資料,但是真正寫出的只是一個位元組的資料,只是
把低八位的位元據寫出,其他二十四位元據全部丟棄。
00000000-000000000-00000001-11111111 511
11111111---> -1
練習:將檔案拷貝至其他路徑下:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/*需求: 拷貝一張圖片。*/public class CopyImage { public static void main(String[] args) throws IOException { //找到目標檔案 File inFile = new File("F:\\美女\\1.jpg"); File destFile = new File("E:\\1.jpg"); //建立資料的輸入輸出通道 FileInputStream fileInputStream = new FileInputStream(inFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加資料.... //每新建立一個FileOutputStream的時候,預設情況下FileOutputStream 的指標是指向了檔案的開始的位置。 每寫出一次,指向都會出現相應移動。 //建立緩衝資料,邊讀邊寫 byte[] buf = new byte[1024]; int length = 0 ; while((length = fileInputStream.read(buf))!=-1){ //最後一次只剩下了824個位元組 fileOutputStream.write(buf,0,length); //寫出很多次資料,所以就必須要追加。 } //關閉資源 原則: 先開後關,後開先關。 fileOutputStream.close(); fileInputStream.close(); }}
異常處理:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import javax.management.RuntimeErrorException;/* IO異常 的處理 */public class Demo1 { public static void main(String[] args) { // readTest(); copyImage(); } // 拷貝圖片 public static void copyImage() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { // 找到目標檔案 File inFile = new File("F:\\美女\\1.jpg"); File outFile = new File("E:\\1.jpg"); // 建立輸入輸出通道 fileInputStream = new FileInputStream(inFile); fileOutputStream = new FileOutputStream(outFile); // 建立緩衝數組,邊讀邊寫 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, length); } } catch (IOException e) { System.out.println("拷貝圖片出錯..."); throw new RuntimeException(e); } finally { // 關閉資源 try { if (fileOutputStream != null) { fileOutputStream.close(); System.out.println("關閉輸出資料流對象成功..."); } } catch (IOException e) { System.out.println("關閉輸出資料流資源失敗..."); throw new RuntimeException(e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); System.out.println("關閉輸入資料流對象成功..."); } catch (IOException e) { System.out.println("關閉輸入資料流對象失敗..."); throw new RuntimeException(e); } } } } } public static void readTest() { FileInputStream fileInputStream = null; try { // 找到目標檔案 File file = new File("F:\\aaaaa.txt"); // 建立資料輸入通道 fileInputStream = new FileInputStream(file); // 建立緩衝數組讀取資料 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, length)); } } catch (IOException e) { /* * //處理的代碼... 首先你要阻止後面的代碼執行,而且要需要通知調用者這裡出錯了... throw new * RuntimeException(e); * //把IOException傳遞給RuntimeException封裝一層,然後再拋出,這樣子做的目的是 * 為了讓調用者使用變得更加靈活。 */ System.out.println("讀取檔案資源出錯...."); throw new RuntimeException(e); } finally { try { if (fileInputStream != null) { fileInputStream.close(); System.out.println("關閉資源成功..."); } } catch (IOException e) { System.out.println("關閉資源失敗..."); throw new RuntimeException(e); } } }}
java之IO輸出位元組流相關操作