天天看點

Java中的IO流-位元組輸出流——FileOutputStream

Java中的IO流——FileOutputStream

1、IO流的分類

 * IO流的分類:

 *         流向:

 *             輸入流 :讀取資料

 *             輸出流 :寫出資料

 *         資料類型:

 *              位元組流

 *                   位元組輸入流 :讀取資料    InputStream

 *                   位元組輸出流 :寫出資料    OutputStream

 *              字元流

 *                   字元輸入流 :讀取資料    Reader

 *                   字元輸出流 :寫出資料    Writer

 *

 *   注意:一般我們在探讨IO流的時候,如果沒有明确說明按哪種分類來說,預設情況下是按照資料類型來分的。

 *

Java中的IO流-位元組輸出流——FileOutputStream
Java中的IO流-位元組輸出流——FileOutputStream
Java中的IO流-位元組輸出流——FileOutputStream
Java中的IO流-位元組輸出流——FileOutputStream

2、 FileOutputStream案例:

需求:我要往一個文本檔案中輸入一句話:"hello,io"

 * 分析:

 *         A:這個操作最好是采用字元流來做,但是呢,字元流是在位元組流之後才出現的,是以,先講解位元組流如何操作。

 *         B:由于是要往檔案中寫一句話,是以我們要采用位元組輸出流。

 *

 * 通過上面的分析後我們知道要使用:OutputStream

 * 但是通過檢視API,我們發現該流對象是一個抽象類,不能執行個體化。

 * 是以,我們要找一個具體的子類。

 * 而我們要找的子類是什麼名字的呢? 這個時候,很簡單,我們回想一下,我們是不是要往檔案中寫東西。

 * 檔案是哪個單詞:File

 * 然後用的是位元組輸出流,聯起來就是:FileOutputStream

 * 注意:每種基類的子類都是以父類名作為字尾名。

(四大基類:OutputStream、InputStream、Reader、Writer)

 *         XxxOutputStream

 *         XxxInputStream

 *         XxxReader

 *         XxxWriter

 * 檢視FileOutputStream的構造方法:

 *         FileOutputStream(File file)

 *        FileOutputStream(String name)

 *

 * 位元組輸出流操作步驟:

 *         A:  建立位元組輸出流對象

 *         B:  寫資料

 *         C:  釋放資源

代碼實作:

package cn.itcast_02;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		// 建立位元組輸出流對象
		// FileOutputStream(File file)
		// File file = new File("fos.txt");
		// FileOutputStream fos = new FileOutputStream(file);
		// FileOutputStream(String name)
		FileOutputStream fos = new FileOutputStream("fos.txt");
		/*
		 * 建立位元組輸出流對象了做了幾件事情: 
		  A:調用系統功能去建立檔案 
		  B:建立fos對象 C:把fos對象指向這個檔案
		 */

		// 寫資料
		fos.write("hello,IO".getBytes());
		fos.write("hello,Java".getBytes());

		// 釋放資源
		// 關閉此檔案輸出流并釋放與此流有關的所有系統資源。
		fos.close();
		/*
		 * 為什麼一定要close()呢? 
		 * A:讓流對象變成垃圾,這樣就可以被垃圾回收器回收了 
		 * B:通知系統去釋放跟該檔案相關的資源
		 */
		// java.io.IOException: Stream Closed
		// fos.write("java".getBytes());
	}
}
           

注意:

        // 釋放資源

        // 關閉此檔案輸出流并釋放與此流有關的所有系統資源。

        fos.close();

         * 為什麼一定要close()呢?

         * A: 讓流對象變成垃圾,這樣就可以被垃圾回收器回收了 。

         * B: 通知系統去釋放跟該檔案相關的資源。

3、FileOutputStream :位元組輸出流的操作步驟、write方法

 * 位元組輸出流操作步驟:

 * A:  建立位元組輸出流對象

 * B:  調用write( )方法

 * C:  釋放資源

 * public void write(int b)  :寫一個位元組

 * public void write(byte[] b)  :寫一個位元組數組

 * public void write(byte[] b,int off,int len)  :寫一個位元組數組的一部分

package cn.itcast_01;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 建立位元組輸出流對象
		// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
		FileOutputStream fos = new FileOutputStream("fos2.txt");

		// 調用write()方法
		// public void write(int b):寫一個位元組
		//fos.write(97); //97 -- 底層二進制資料	-- 通過記事本打開 -- 找97對應的字元值 -- a
		// fos.write(57);
		// fos.write(55);
		
		//public void write(byte[] b):寫一個位元組數組
		byte[] bys={97,98,99,100,101};
		fos.write(bys);
		
		//public void write(byte[] b,int off,int len):寫一個位元組數組的一部分
		fos.write(bys,1,3);
		
		//釋放資源
		fos.close();
	}
}
           

4、加入異常處理的位元組輸出流操作:FileOutputStream

package cn.itcast_01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入異常處理的位元組輸出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 分開做異常處理
		// FileOutputStream fos = null;
		// try {
		// fos = new FileOutputStream("fos4.txt");
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.write("java".getBytes());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 一起做異常處理
		// try {
		// FileOutputStream fos = new FileOutputStream("fos4.txt");
		// fos.write("java".getBytes());
		// fos.close();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 改進版
		// 為了在finally裡面能夠看到該對象就必須定義到外面,為了通路不出問題,還必須給初始化值
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 為了保證close()一定會執行,就放到這裡了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
           

開發版本**********************************

package cn.itcast_01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入異常處理的位元組輸出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 改進版
		// 為了在finally裡面能夠看到該對象就必須定義到外面,為了通路不出問題,還必須給初始化值。
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 為了保證close()一定會執行,就放到這裡了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}