1. 程式人生 > >【JavaSE學習筆記】IO流02_位元組輸出流OutputStream、位元組輸入流InputStream

【JavaSE學習筆記】IO流02_位元組輸出流OutputStream、位元組輸入流InputStream

IO流02

概述

1)IO流:裝置和裝置之間的傳輸(讀寫)

2)分類

按流的方向分為:

輸入流   --->讀取資料

輸出流   --->寫出資料

按資料型別分為:

位元組輸入流:InputStream          ----讀資料

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

字元輸入流:Reader

字元輸出流:Writer

3)需求:寫一個檔案,這個檔案內容是:hello,io

分析:寫這樣一個檔案:最常用的是使用字元流

但是字元流實在位元組流之後才產生的

所以:先講位元組流中的位元組輸出流,後面講字元流

A.位元組輸出流OutputStream

1)概述

一個抽象類,不能被例項化

使用他的物件進行寫資料

現在的需求:針對的是檔案:File 聯想起來:FileOutputStream

對於輸出流和輸入流來說,他們的基類的子類的字尾名是以下形式

XXXOutputStream
XXXInputStream
XXXReader
xxxWriter

2)些資料的具體步驟:

a.建立位元組輸出流物件

構造方法:

public FileOutputStream(File file):指定一個File物件
public FileOutputStream(String name):直接指定檔名稱(常用)

b.寫資料

c.釋放資源

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

public class Demo01 {
	public static void main(String[] args) throws IOException {
		/**
		 * 建立檔案位元組流輸出物件做了幾件事情? 1)呼叫系統功能區建立FileOutputStream物件 2)將fos物件指向fos.txt檔案
		 */
		// 丟擲異常,在這為了方便演示,在main方法上直接拋棄(後面再講正確使用方法)
		FileOutputStream fos = new FileOutputStream("fos.txt");

		// 寫資料:
		// 位元組流:應該寫一個位元組進去
		fos.write("hello,io".getBytes());
		fos.write("\r\n".getBytes());
		// windows換行\r\n; Linux \n; Mac \r
		fos.write("java".getBytes());

		// 釋放資源
		// 關閉資源
		// 關閉和IO流物件有關的資源
		fos.close();

		// 資源已經關閉
		// fos.write("java".getBytes());
		// java.io.IOException: Stream Closed:如果流資源已經關閉,無須在去寫了!
	}
}



3)檔案位元組輸出流物件中的寫資料的方法

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

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

public void write(byte[] b, int index,int len):寫一部分位元組陣列(常用)

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

public class Demo02 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("fos.txt");

		// public void write(int b):寫入一個位元組
		// fos.write(97);---->計算出二進位制資料---->記事本開啟之後----->找ASCII對應的字元:97 a
		// fos.write(55);
		// fos.write(57);

		// public void write(byte[] b):寫一個位元組陣列
		// 定義一個位元組數陣列
		byte[] bys = { 97, 98, 99, 100, 101 };
		// 寫資料
		// fos.write(bys);

		// public void write(byte[] b, int index,int len):寫一部分位元組陣列
		fos.write(bys, 1, 3); // 98 99 100 對應的ASCII表

		// 關閉資源
		fos.close();
	}
}

4)換行

針對不同的作業系統換行符號是不一樣的:

windows:\r\n
Linux:\n
Mac:\r

5)追加操作

public FileOutputStream(File file, boolean append):

如果第二個引數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處

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

public class Demo03 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("fos.txt", true);

		// 寫資料
		for (int i = 0; i < 10; i++) {
			fos.write(("\r\n" + i + "hello").getBytes());
		}

		// 釋放資源
		fos.close();
	}
}


6)正確的異常處理方式

檔案位元組輸出流中加入異常處理(try...catch方式)

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

public class Demo04 {
	public static void main(String[] args) {
		// 為了fos物件在finlly中能看到,所以把fos物件定義外面;
		FileOutputStream fos = null;

		try {
			fos = new FileOutputStream("fos");
			// 寫資料
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//關閉資源,但是資源之前,首先判斷資源物件是否為空
			if (fos != null) {
				// 開始關閉資源
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

7)練習

輸出九九乘法表

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

public class Demo05 {
	public static void main(String[] args) {
		FileOutputStream fos = null;

		try {
			fos = new FileOutputStream("九九乘法表");
			fos.write("\t\t\t\t\t\t\t九九乘法表\r\n".getBytes());
			for (int i = 1; i <= 9; i++) {
				for (int j = 1; j <= i; j++) {
					fos.write((j + "*" + i + "=" + j * i + "\t").getBytes());
				}
				fos.write("\r\n".getBytes());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

B.位元組輸入流InputStream

1)概述

建立輸入流物件

讀資料:通過讀取檔案,顯示在控制檯

釋放資源

2)構造方法

public int read():一次讀取一個位元組


讀取

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo01 {
	public static void main(String[] args) {
		// 建立位元組輸入流物件
		FileInputStream fis = null;

		try {
			fis = new FileInputStream("fis.txt");
			// //一次讀取一個位元組
			// int by = fis.read();
			// System.out.println((char)by);
			// //.....要讀很多次
			/**
			 * 重複都高,並且判斷的結束條件讀到-1,說明已經 讀完了
			 */
			int by = 0;
			while ((by = fis.read()) != -1) {
				System.out.print((char)by);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


public int read(byte[] b):一次讀取一個位元組陣列


讀取

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo02 {
	public static void main(String[] args) {
		FileInputStream fis = null;

		try {
			fis = new FileInputStream("fis.txt");

			// 讀取資料:給定位元組陣列的長度是1024或者1024的倍數
			byte[] bys = new byte[1024];
			// 定義長度
			int len = 0;
			while ((len = fis.read(bys)) != -1) {
				// 不能直接輸出(new String(bys)
				// 要從0到len
				System.out.print(new String(bys, 0, len));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


3)關於漢子

讀取漢子的話,漢子不能處在陣列長度的界限上,否則漢子會成亂碼

一個漢字佔兩個位元組,但要設定三個長度

例如(你好嗎)  new byte[n]  n = 3 || n >= 6讀取正常   

若(a你好嗎) new byte[n]  n >= 7讀取正常   

4)練習

讀取九九乘法表


"九九乘法表"這五個字肯定在1024範圍內,所以位元組陣列長度正常設定就行

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo03 {
	public static void main(String[] args) {
		FileInputStream fis = null;

		try {
			fis = new FileInputStream("九九乘法表.txt");
			byte[] bys = new byte[1024];
			int len = 0;
			while ((len = fis.read(bys)) != -1) {
				System.out.print(new String(bys, 0, len));
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}