API-IO流

API-IO

學習內容

  1. 異常

  2. File

  3. 遞歸

  4. 字節流

  5. 轉換流

  6. 字符流

  7. 其他流


一、異常

異常:異常就是Java程序在運行過程中出現的錯誤。

異常由來:問題也是現實生活中一個具體事務,也可以通過java的類的形式進行描述,並封裝成對象。其實就是Java對不正常情況進行描述後的對象體現。

我們見過的異常,角標越界異常,空指針異常

/*
 * 舉例:今天天氣很好,班長出去旅遊。騎着自行車,去山裏面呼吸新鮮空氣。
 * 		問題1:山路塌陷了,班長及時停住了,但是過不去了。嚴重的問題。
 * 		問題2:班長出門推自行車,發現氣沒了,把氣吹起來。出發前就應該檢查的問題。
 * 		問題3:班長騎着車在山路上愜意的行駛着,山路兩邊是有小石子的,中間是平坦的水泥路。
 * 			一直在平坦的水泥路上行駛是沒有任何問題的,但是呢,他偏偏喜歡騎到小石子上,結果爆胎了。旅遊的過程中出現的問題。
 * 			no zuo no die。
 * 
 * 程序的異常:Throwable
 * 		嚴重問題:Error 我們不處理。這種問題一般都是很嚴重的,比如說內存溢出。
 * 		問題:Exception
 * 			編譯期問題:不是RuntimeException的異常 必須進行處理的,因爲你不處理,編譯就不能通過。
 * 			運行期問題:RuntimeException	這種問題我們也不處理,因爲是你的問題,而且這個問題出現肯定是我們的代碼不夠嚴謹,需要修正代碼的。
 * 
 * 如何程序出現了問題,我們沒有做任何處理,最終jvm會做出默認的處理。
 * 把異常的名稱,原因及出現的問題等信息輸出在控制檯。
 * 同時會結束程序。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		//第一階段
		int a = 10;
		// int b = 2;
		int b = 0;
		System.out.println(a / b);
		
		//第二階段
		System.out.println("over");
	}
}

 

異常分類

Throwable

  • |--Error 嚴重問題,我們不處理。

  • |--Exception

  • |--RuntimeException 運行期異常,我們需要修正代碼

  • |--非RuntimeException 編譯期異常,必須處理的,否則程序編譯不通過

 

異常舉例

除數爲0

數組訪問越界

 

JVM的默認處理方案

把異常的名稱,錯誤原因及異常出現的位置等信息輸出在了控制檯

程序停止執行

 

異常處理方案

異常處理方案

•    trycatchfinally

•    throws

/*
 * 我們自己如何處理異常呢?
 * A:try...catch...finally
 * B:throws 拋出
 * 
 * try...catch...finally的處理格式:
 * 		try {
 * 			可能出現問題的代碼;
 * 		}catch(異常名 變量) {
 * 			針對問題的處理;
 * 		}finally {
 * 			釋放資源;
 * 		}
 * 
 * 變形格式:
 * 		try {
 * 			可能出現問題的代碼;
 * 		}catch(異常名 變量) {
 * 			針對問題的處理;
 * 		}
 * 
 * 注意:
 * 		A:try裏面的代碼越少越好
 * 		B:catch裏面必須有內容,哪怕是給出一個簡單的提示
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// 第一階段
		int a = 10;
		// int b = 2;
		int b = 0;

		try {
			System.out.println(a / b);
		} catch (ArithmeticException ae) {
			System.out.println("除數不能爲0");
		}

		// 第二階段
		System.out.println("over");
	}
}

 

trycatch處理方式

一個異常的情況

多個異常的情況

•    平級情況

•    存在上下級的情況

/*
 * A:一個異常
 * B:二個異常的處理
 * 		a:每一個寫一個try...catch
 * 		b:寫一個try,多個catch
 * 			try{
 * 				...
 * 			}catch(異常類名 變量名) {
 * 				...
 * 			}
 * 			catch(異常類名 變量名) {
 * 				...
 * 			}
 * 			...
 * 
 * 			注意事項:
 * 				1:能明確的儘量明確,不要用大的來處理。
 * 				2:平級關係的異常誰前誰後無所謂,如果出現了子父關係,父必須在後面。
 * 
 * 注意:
 * 		一旦try裏面出了問題,就會在這裏把問題給拋出去,然後和catch裏面的問題進行匹配,
 * 		一旦有匹配的,就執行catch裏面的處理,然後結束了try...catch
 * 		繼續執行後面的語句。
 */
public class ExceptionDemo2 {
	public static void main(String[] args) {
		// method1();

		// method2();

		// method3();

		method4();
	}

	public static void method4() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		// 爺爺在最後
		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
			System.out.println("這裏出現了一個異常,你不太清楚是誰,該怎麼辦呢?");
		} catch (ArithmeticException e) {
			System.out.println("除數不能爲0");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你訪問了不該的訪問的索引");
		} catch (Exception e) {
			System.out.println("出問題了");
		}

		// 爺爺在前面是不可以的
		// try {
		// System.out.println(a / b);
		// System.out.println(arr[3]);
		// System.out.println("這裏出現了一個異常,你不太清楚是誰,該怎麼辦呢?");
		// } catch (Exception e) {
		// System.out.println("出問題了");
		// } catch (ArithmeticException e) {
		// System.out.println("除數不能爲0");
		// } catch (ArrayIndexOutOfBoundsException e) {
		// System.out.println("你訪問了不該的訪問的索引");
		// }

		System.out.println("over");
	}

	// 兩個異常的處理
	public static void method3() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		try {
			System.out.println(arr[3]);
			System.out.println(a / b);
			// System.out.println(arr[3]);
		} catch (ArithmeticException e) {
			System.out.println("除數不能爲0");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你訪問了不該的訪問的索引");
		}

		System.out.println("over");
	}

	// 兩個異常
	public static void method2() {
		int a = 10;
		int b = 0;
		try {
			System.out.println(a / b);
		} catch (ArithmeticException e) {
			System.out.println("除數不能爲0");
		}

		int[] arr = { 1, 2, 3 };
		try {
			System.out.println(arr[3]);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你訪問了不該的訪問的索引");
		}

		System.out.println("over");
	}

	// 一個異常
	public static void method1() {
		// 第一階段
		int a = 10;
		// int b = 2;
		int b = 0;

		try {
			System.out.println(a / b);
		} catch (ArithmeticException ae) {
			System.out.println("除數不能爲0");
		}

		// 第二階段
		System.out.println("over");
	}
}

•    JDK7的新特性及注意事項

/*
 * JDK7出現了一個新的異常處理方案:
 * 		try{
 * 
 * 		}catch(異常名1 | 異常名2 | ...  變量 ) {
 * 			...
 * 		}
 * 
 * 		注意:這個方法雖然簡潔,但是也不夠好。
 * 			A:處理方式是一致的。(實際開發中,好多時候可能就是針對同類型的問題,給出同一個處理)
 *			B:多個異常間必須是平級關係。
 */
public class ExceptionDemo3 {
	public static void main(String[] args) {
		method();
	}

	public static void method() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		// try {
		// System.out.println(a / b);
		// System.out.println(arr[3]);
		// System.out.println("這裏出現了一個異常,你不太清楚是誰,該怎麼辦呢?");
		// } catch (ArithmeticException e) {
		// System.out.println("除數不能爲0");
		// } catch (ArrayIndexOutOfBoundsException e) {
		// System.out.println("你訪問了不該的訪問的索引");
		// } catch (Exception e) {
		// System.out.println("出問題了");
		// }

		// JDK7的處理方案
		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("出問題了");
		}

		System.out.println("over");
	}

}

 

編譯時異常和運行時異常的區別

Java中的異常被分爲兩大類:編譯時異常和運行時異常。所有的RuntimeException類及其子類的實例被稱爲運行時異常,其他的異常就是編譯時異常

編譯時異常

•    Java程序必須顯示處理,否則程序就會發生錯誤,無法通過編譯

運行時異常

•    無需顯示處理,也可以和編譯時異常一樣處理

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 編譯時異常和運行時異常的區別
 * 編譯期異常:Java程序必須顯示處理,否則程序就會發生錯誤,無法通過編譯
 * 運行期異常:無需顯示處理,也可以和編譯時異常一樣處理
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// int a = 10;
		// int b = 0;
		// if (b != 0) {
		// System.out.println(a / b);
		// }

		String s = "2014-11-20";
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// Date d = sdf.parse(s);
		try {
			Date d = sdf.parse(s);
			System.out.println(d);
		} catch (ParseException e) {
			// e.printStackTrace();
			System.out.println("解析日期出問題了");
		}
	}
}

 

Throwable中的方法

getMessage()

•     獲取異常信息,返回字符串。

toString()

•     獲取異常類名和異常信息,返回字符串。

printStackTrace()

•     獲取異常類名和異常信息,以及異常出現在程序中的位置。返回值void

printStackTrace(PrintStream s)

•     通常用該方法將異常內容保存在日誌文件中,以便查閱。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 在try裏面發現問題後,jvm會幫我們生成一個異常對象,然後把這個對象拋出,和catch裏面的類進行匹配。
 * 如果該對象是某個類型的,就會執行該catch裏面的處理信息。
 * 
 * 異常中要了解的幾個方法:
 * public String getMessage():異常的消息字符串		
 * public String toString():返回異常的簡單信息描述
 * 		此對象的類的 name(全路徑名)
 * 		": "(冒號和一個空格) 
 * 		調用此對象 getLocalizedMessage()方法的結果 (默認返回的是getMessage()的內容)
 * printStackTrace() 獲取異常類名和異常信息,以及異常出現在程序中的位置。返回值void。把信息輸出在控制檯。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = sdf.parse(s); // 創建了一個ParseException對象,然後拋出去,和catch裏面進行匹配
			System.out.println(d);
		} catch (ParseException e) { // ParseException e = new ParseException();
			// ParseException
			// e.printStackTrace();

			// getMessage()
			// System.out.println(e.getMessage());
			// Unparseable date: "2014-11-20"

			// toString()
			// System.out.println(e.toString());
			// java.text.ParseException: Unparseable date: "2014-11-20"
			
			e.printStackTrace();
			//跳轉到某個指定的頁面(index.html)
		}
		
		System.out.println("over");
	}
}

 

throws

定義功能方法時,需要把出現的問題暴露出來讓調用者去處理。那麼就通過throws在方法上標識。

舉例分別演示編譯時異常和運行時異常的拋出

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 有些時候,我們是可以對異常進行處理的,但是又有些時候,我們根本就沒有權限去處理某個異常。
 * 或者說,我處理不了,我就不處理了。
 * 爲了解決出錯問題,Java針對這種情況,就提供了另一種處理方案:拋出。
 * 
 * 格式:
 * 		throws 異常類名
 * 		注意:這個格式必須跟在方法的括號後面。
 * 
 * 注意:
 * 		儘量不要在main方法上拋出異常。
 * 		但是我講課爲了方便我就這樣做了。
 * 
 * 小結:
 * 		編譯期異常拋出,將來調用者必須處理。
 * 		運行期異常拋出,將來調用可以不用處理。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		System.out.println("今天天氣很好");
		try {
			method();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("但是就是不該有霧霾");

		method2();
	}

	// 運行期異常的拋出
	public static void method2() throws ArithmeticException {
		int a = 10;
		int b = 0;
		System.out.println(a / b);
	}

	// 編譯期異常的拋出
	// 在方法聲明上拋出,是爲了告訴調用者,你注意了,我有問題。
	public static void method() throws ParseException {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(s);
		System.out.println(d);
	}
}

 

throwsthrow的區別

throws

•     用在方法聲明後面,跟的是異常類名

•     可以跟多個異常類名,用逗號隔開

•     表示拋出異常,由該方法的調用者來處理

•     throws表示出現異常的一種可能性,並不一定會發生這些異常

throw

•     用在方法體內,跟的是異常對象名

•     只能拋出一個異常對象名

•     表示拋出異常,由方法體內的語句處理

•      throw則是拋出了異常,執行throw則一定拋出了某種異常 

/*
 * throw:如果出現了異常情況,我們可以把該異常拋出,這個時候的拋出的應該是異常的對象。
 * 
 * throws和throw的區別(面試題)
	throws
		用在方法聲明後面,跟的是異常類名
		可以跟多個異常類名,用逗號隔開
		表示拋出異常,由該方法的調用者來處理
		throws表示出現異常的一種可能性,並不一定會發生這些異常
	throw
		用在方法體內,跟的是異常對象名
		只能拋出一個異常對象名
		表示拋出異常,由方法體內的語句處理
		throw則是拋出了異常,執行throw則一定拋出了某種異常
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// method();
		
		try {
			method2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void method() {
		int a = 10;
		int b = 0;
		if (b == 0) {
			throw new ArithmeticException();
		} else {
			System.out.println(a / b);
		}
	}

	public static void method2() throws Exception {
		int a = 10;
		int b = 0;
		if (b == 0) {
			throw new Exception();
		} else {
			System.out.println(a / b);
		}
	}
}

 

我們到底該如何處理異常呢

原則:如果該功能內部可以將問題處理,try,如果處理不了,交由調用者處理,這是用throws

區別:

•     後續程序需要繼續運行就try

•     後續程序不需要繼續運行就throws

舉例:

•     感冒了就自己吃點藥就好了,try

•     吃了好幾天藥都沒好結果得了H7N9,那就的得throws到醫院

•     如果醫院沒有特效藥就變成Error

 

finally的特點作用及面試題

finally的特點

•    finally控制的語句體一定會執行

•    特殊情況:在執行到finally之前jvm退出了(比如System.exit(0))

finally的作用

•    用於釋放資源,在IO流操作和數據庫操作中會見到

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * finally:被finally控制的語句體一定會執行
 * 注意:如果在執行到finally之前jvm退出了,就不能執行了。
 * 
 * A:格式
 * 		try...catch...finally...
 * B:用於釋放資源,在IO流操作和數據庫操作中會見到
 */
public class FinallyDemo {
	public static void main(String[] args) {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date d = null;
		try {
			// System.out.println(10 / 0);
			d = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			System.out.println("這裏的代碼是可以執行的");
		}

		System.out.println(d);
	}
}

finally相關的面試題

•    final,finallyfinalize的區別

•    如果catch裏面有return語句,請問finally的代碼還會執行嗎?如果會,請問是在return前還是return後。

/*
 * 面試題:
 * 1:final,finally和finalize的區別
 * final:最終的意思,可以修飾類,成員變量,成員方法
 * 		修飾類,類不能被繼承
 * 		修飾變量,變量是常量
 * 		修飾方法,方法不能被重寫
 * finally:是異常處理的一部分,用於釋放資源。
 * 		一般來說,代碼肯定會執行,特殊情況:在執行到finally之前jvm退出了
 * finalize:是Object類的一個方法,用於垃圾回收
 * 
 * 2:如果catch裏面有return語句,請問finally裏面的代碼還會執行嗎?
 *   如果會,請問是在return前,還是return後。
 * 	   會。前。
 * 
 * 	 準確的說,應該是在中間。
 * 
 * 3:try...catch...finally的格式變形
 * 		A:try...catch...finally
 * 		B:try...catch
 * 		C:try...catch...catch...
 * 		D:try...catch...catch...finally
 * 		E:try...finally
 * 			這種做法的目前是爲了釋放資源。
 */
public class FinallyDemo2 {
	public static void main(String[] args) {
		System.out.println(getInt());
	}

	public static int getInt() {
		int a = 10;
		try {
			System.out.println(a / 0);
			a = 20;
		} catch (ArithmeticException e) {
			a = 30;
			return a;
			/*
			 * return a在程序執行到這一步的時候,這裏不是return a而是return 30;這個返回路徑就形成了。
			 * 但是呢,它發現後面還有finally,所以繼續執行finally的內容,a=40
			 * 再次回到以前的返回路徑,繼續走return 30;
			 */
		} finally {
			a = 40;
			return a;//如果這樣結果就是40了。
		}
		// return a;
	}
}

 

自定義異常

考試成績必須在0-100之間

很明顯java沒有對應的異常,需要我們自己來做一個異常

自定義異常

•     繼承自Exception

•      繼承自RuntimeException

/*
 * java不可能對所有的情況都考慮到,所以,在實際的開發中,我們可能需要自己定義異常。
 * 而我們自己隨意的寫一個類,是不能作爲異常類來看的,要想你的類是一個異常類,就必須繼承自Exception或者RuntimeException
 * 
 * 兩種方式:
 * A:繼承Exception
 * B:繼承RuntimeException
 */
public class MyException extends Exception {
	public MyException() {
	}

	public MyException(String message) {
		super(message);
	}
}

// public class MyException extends RuntimeException {
//
// }

 

異常注意事項

l   子類重寫父類方法時,子類的方法必須拋出相同的異常或父類異常的子類。(父親壞了,兒子不能比父親更壞)

l   如果父類拋出了多個異常,子類重寫父類時,只能拋出相同的異常或者是他的子集,子類不能拋出父類沒有的異常

l   如果被重寫的方法沒有異常拋出,那麼子類的方法絕對不可以拋出異常,如果子類方法內有異常發生,那麼子類只能try,不能throws

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 異常注意事項:
 * A:子類重寫父類方法時,子類的方法必須拋出相同的異常或父類異常的子類。(父親壞了,兒子不能比父親更壞)
 * B:如果父類拋出了多個異常,子類重寫父類時,只能拋出相同的異常或者是他的子集,子類不能拋出父類沒有的異常
 * C:如果被重寫的方法沒有異常拋出,那麼子類的方法絕對不可以拋出異常,如果子類方法內有異常發生,那麼子類只能try,不能throws
 */
public class ExceptionDemo {

}

class Fu {
	public void show() throws Exception {
	}

	public void method() {
	}
}

class Zi extends Fu {
	@Override
	public void show() throws ArithmeticException {

	}

	@Override
	public void method() {
		// String s = "2014-11-20";
		// SimpleDateFormat sdf = new SimpleDateFormat();
		// Date d = sdf.parse(s);
		// System.out.println(d);
	}
}

二、File類

File類的概述

•    文件和目錄路徑名的抽象表示形式

構造方法

•    publicFile(String pathname)

•    publicFile(String parent,String child)

•    publicFile(File parent,String child)

import java.io.File;

/*
 * 我們要想實現IO的操作,就必須知道硬盤上文件的表現形式。
 * 而Java就提供了一個類File供我們使用。
 * 
 * File:文件和目錄(文件夾)路徑名的抽象表示形式
 * 構造方法:
 * 		File(String pathname):根據一個路徑得到File對象
 * 		File(String parent, String child):根據一個目錄和一個子文件/目錄得到File對象
 * 		File(File parent, String child):根據一個父File對象和一個子文件/目錄得到File對象
 */
public class FileDemo {
	public static void main(String[] args) {
		// File(String pathname):根據一個路徑得到File對象
		// 把e:\\demo\\a.txt封裝成一個File對象
		File file = new File("E:\\demo\\a.txt");

		// File(String parent, String child):根據一個目錄和一個子文件/目錄得到File對象
		File file2 = new File("E:\\demo", "a.txt");

		// File(File parent, String child):根據一個父File對象和一個子文件/目錄得到File對象
		File file3 = new File("e:\\demo");
		File file4 = new File(file3, "a.txt");

		// 以上三種方式其實效果一樣
	}
}

 

File類的成員方法

創建功能

•    public boolean createNewFile()

•    public boolean mkdir()

•    public boolean mkdirs()

import java.io.File;
import java.io.IOException;

/*
 *創建功能:
 *public boolean createNewFile():創建文件 如果存在這樣的文件,就不創建了
 *public boolean mkdir():創建文件夾 如果存在這樣的文件夾,就不創建了
 *public boolean mkdirs():創建文件夾,如果父文件夾不存在,會幫你創建出來
 *
 *騎白馬的不一定是王子,可能是班長。
 *注意:你到底要創建文件還是文件夾,你最清楚,方法不要調錯了。
 */
public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 需求:我要在e盤目錄下創建一個文件夾demo
		File file = new File("e:\\demo");
		System.out.println("mkdir:" + file.mkdir());

		// 需求:我要在e盤目錄demo下創建一個文件a.txt
		File file2 = new File("e:\\demo\\a.txt");
		System.out.println("createNewFile:" + file2.createNewFile());

		// 需求:我要在e盤目錄test下創建一個文件b.txt
		// Exception in thread "main" java.io.IOException: 系統找不到指定的路徑。
		// 注意:要想在某個目錄下創建內容,該目錄首先必須存在。
		// File file3 = new File("e:\\test\\b.txt");
		// System.out.println("createNewFile:" + file3.createNewFile());

		// 需求:我要在e盤目錄test下創建aaa目錄
		// File file4 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file4.mkdir());

		// File file5 = new File("e:\\test");
		// File file6 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file5.mkdir());
		// System.out.println("mkdir:" + file6.mkdir());

		// 其實我們有更簡單的方法
		File file7 = new File("e:\\aaa\\bbb\\ccc\\ddd");
		System.out.println("mkdirs:" + file7.mkdirs());

		// 看下面的這個東西:
		File file8 = new File("e:\\liuyi\\a.txt");
		System.out.println("mkdirs:" + file8.mkdirs());
	}
}

刪除功能

•    public boolean delete()

import java.io.File;
import java.io.IOException;

/*
 * 刪除功能:public boolean delete()
 * 
 * 注意:
 * 		A:如果你創建文件或者文件夾忘了寫盤符路徑,那麼,默認在項目路徑下。
 * 		B:Java中的刪除不走回收站。
 * 		C:要刪除一個文件夾,請注意該文件夾內不能包含文件或者文件夾
 */
public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 創建文件
		// File file = new File("e:\\a.txt");
		// System.out.println("createNewFile:" + file.createNewFile());

		// 我不小心寫成這個樣子了
		File file = new File("a.txt");
		System.out.println("createNewFile:" + file.createNewFile());

		// 繼續玩幾個
		File file2 = new File("aaa\\bbb\\ccc");
		System.out.println("mkdirs:" + file2.mkdirs());

		// 刪除功能:我要刪除a.txt這個文件
		File file3 = new File("a.txt");
		System.out.println("delete:" + file3.delete());

		// 刪除功能:我要刪除ccc這個文件夾
		File file4 = new File("aaa\\bbb\\ccc");
		System.out.println("delete:" + file4.delete());

		// 刪除功能:我要刪除aaa文件夾
		// File file5 = new File("aaa");
		// System.out.println("delete:" + file5.delete());

		File file6 = new File("aaa\\bbb");
		File file7 = new File("aaa");
		System.out.println("delete:" + file6.delete());
		System.out.println("delete:" + file7.delete());
	}
}

重命名功能

•    public boolean renameTo(File dest)

import java.io.File;

/*
 * 重命名功能:public boolean renameTo(File dest)
 * 		如果路徑名相同,就是改名。
 * 		如果路徑名不同,就是改名並剪切。
 * 
 * 路徑以盤符開始:絕對路徑	c:\\a.txt
 * 路徑不以盤符開始:相對路徑	a.txt
 */
public class FileDemo {
	public static void main(String[] args) {
		// 創建一個文件對象
		// File file = new File("林青霞.jpg");
		// // 需求:我要修改這個文件的名稱爲"東方不敗.jpg"
		// File newFile = new File("東方不敗.jpg");
		// System.out.println("renameTo:" + file.renameTo(newFile));

		File file2 = new File("東方不敗.jpg");
		File newFile2 = new File("e:\\林青霞.jpg");
		System.out.println("renameTo:" + file2.renameTo(newFile2));
	}
}

 

File類的成員方法

判斷功能

•    public boolean isDirectory()

•    public boolean isFile()

•    public boolean exists()

•    public boolean canRead()

•    public boolean canWrite()

•    public boolean isHidden()

import java.io.File;

/*
 * 判斷功能:
 * public boolean isDirectory():判斷是否是目錄
 * public boolean isFile():判斷是否是文件
 * public boolean exists():判斷是否存在
 * public boolean canRead():判斷是否可讀
 * public boolean canWrite():判斷是否可寫
 * public boolean isHidden():判斷是否隱藏
 */
public class FileDemo {
	public static void main(String[] args) {
		// 創建文件對象
		File file = new File("a.txt");

		System.out.println("isDirectory:" + file.isDirectory());// false
		System.out.println("isFile:" + file.isFile());// true
		System.out.println("exists:" + file.exists());// true
		System.out.println("canRead:" + file.canRead());// true
		System.out.println("canWrite:" + file.canWrite());// true
		System.out.println("isHidden:" + file.isHidden());// false
	}
}

 

File類的成員方法

基本獲取功能

•    public String getAbsolutePath()

•    public String getPath()

•    public String getName()

•    public long length()

•    public long lastModified()

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 獲取功能:
 * public String getAbsolutePath():獲取絕對路徑
 * public String getPath():獲取相對路徑
 * public String getName():獲取名稱
 * public long length():獲取長度。字節數
 * public long lastModified():獲取最後一次的修改時間,毫秒值
 */
public class FileDemo {
	public static void main(String[] args) {
		// 創建文件對象
		File file = new File("demo\\test.txt");

		System.out.println("getAbsolutePath:" + file.getAbsolutePath());
		System.out.println("getPath:" + file.getPath());
		System.out.println("getName:" + file.getName());
		System.out.println("length:" + file.length());
		System.out.println("lastModified:" + file.lastModified());

		// 1416471971031
		Date d = new Date(1416471971031L);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sdf.format(d);
		System.out.println(s);
	}
}

 

高級獲取功能

•    public String[] list()

•    public File[] listFiles()

import java.io.File;

/*
 * 獲取功能:
 * public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數組
 * public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數組
 */
public class FileDemo {
	public static void main(String[] args) {
		// 指定一個目錄
		File file = new File("e:\\");

		// public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數組
		String[] strArray = file.list();
		for (String s : strArray) {
			System.out.println(s);
		}
		System.out.println("------------");

		// public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數組
		File[] fileArray = file.listFiles();
		for (File f : fileArray) {
			System.out.println(f.getName());
		}
	}
}

File類練習

判斷E盤目錄下是否有後綴名爲.jpg的文件,如果有,就輸出此文件名稱

import java.io.File;

/*
 * 判斷E盤目錄下是否有後綴名爲.jpg的文件,如果有,就輸出此文件名稱
 * 
 * 分析:
 * 		A:封裝e判斷目錄
 * 		B:獲取該目錄下所有文件或者文件夾的File數組
 * 		C:遍歷該File數組,得到每一個File對象,然後判斷
 * 		D:是否是文件
 * 			是:繼續判斷是否以.jpg結尾
 * 				是:就輸出該文件名稱
 * 				否:不搭理它
 * 			否:不搭理它
 */
public class FileDemo {
	public static void main(String[] args) {
		// 封裝e判斷目錄
		File file = new File("e:\\");

		// 獲取該目錄下所有文件或者文件夾的File數組
		File[] fileArray = file.listFiles();

		// 遍歷該File數組,得到每一個File對象,然後判斷
		for (File f : fileArray) {
			// 是否是文件
			if (f.isFile()) {
				// 繼續判斷是否以.jpg結尾
				if (f.getName().endsWith(".jpg")) {
					// 就輸出該文件名稱
					System.out.println(f.getName());
				}
			}
		}
	}
}

 

文件名稱過濾器的實現思想及代碼

•     public String[] list(FilenameFilter filter)

•     public File[] listFiles(FilenameFilter filter)

•     查看源碼看執行流程

import java.io.File;
import java.io.FilenameFilter;

/*
 * 判斷E盤目錄下是否有後綴名爲.jpg的文件,如果有,就輸出此文件名稱
 * A:先獲取所有的,然後遍歷的時候,依次判斷,如果滿足條件就輸出。
 * B:獲取的時候就已經是滿足條件的了,然後輸出即可。
 * 
 * 要想實現這個效果,就必須學習一個接口:文件名稱過濾器
 * public String[] list(FilenameFilter filter)
 * public File[] listFiles(FilenameFilter filter)
 */
public class FileDemo2 {
	public static void main(String[] args) {
		// 封裝e判斷目錄
		File file = new File("e:\\");

		// 獲取該目錄下所有文件或者文件夾的String數組
		// public String[] list(FilenameFilter filter)
		String[] strArray = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				// return false;
				// return true;
				// 通過這個測試,我們就知道了,到底把這個文件或者文件夾的名稱加不加到數組中,取決於這裏的返回值是true還是false
				// 所以,這個的true或者false應該是我們通過某種判斷得到的
				// System.out.println(dir + "---" + name);
				// File file = new File(dir, name);
				// // System.out.println(file);
				// boolean flag = file.isFile();
				// boolean flag2 = name.endsWith(".jpg");
				// return flag && flag2;
				return new File(dir, name).isFile() && name.endsWith(".jpg");
			}
		});

		// 遍歷
		for (String s : strArray) {
			System.out.println(s);
		}
	}
}

|  案例

import java.io.File;

/*
 * 需求:把E:\評書\三國演義下面的視頻名稱修改爲
 * 		00?_介紹.avi
 * 
 * 思路:
 * 		A:封裝目錄
 * 		B:獲取該目錄下所有的文件的File數組
 * 		C:遍歷該File數組,得到每一個File對象
 * 		D:拼接一個新的名稱,然後重命名即可。
 */
public class FileDemo {
	public static void main(String[] args) {
		// 封裝目錄
		File srcFolder = new File("E:\\評書\\三國演義");

		// 獲取該目錄下所有的文件的File數組
		File[] fileArray = srcFolder.listFiles();

		// 遍歷該File數組,得到每一個File對象
		for (File file : fileArray) {
			// System.out.println(file);
			// E:\評書\三國演義\三國演義_001_[評書網-今天很高興,明天就IO了]_桃園三結義.avi
			// 改後:E:\評書\三國演義\001_桃園三結義.avi
			String name = file.getName(); // 三國演義_001_[評書網-今天很高興,明天就IO了]_桃園三結義.avi

			int index = name.indexOf("_");
			String numberString = name.substring(index + 1, index + 4);
			// System.out.println(numberString);

			// int startIndex = name.lastIndexOf('_');
			// int endIndex = name.lastIndexOf('.');
			// String nameString = name.substring(startIndex + 1, endIndex);
			// System.out.println(nameString);
			int endIndex = name.lastIndexOf('_');
			String nameString = name.substring(endIndex);

			String newName = numberString.concat(nameString); // 001_桃園三結義.avi
			// System.out.println(newName);

			File newFile = new File(srcFolder, newName); // E:\\評書\\三國演義\\001_桃園三結義.avi

			// 重命名即可
			file.renameTo(newFile);
		}
	}
}

三、遞歸的思想

遞歸

•    方法定義中調用方法本身的現象

遞歸注意實現

•    要有出口,否則就是死遞歸

•    次數不能太多,否則就內存溢出

•    構造方法不能遞歸使用

/*
 * 遞歸:方法定義中調用方法本身的現象
 * 
 * 方法的嵌套調用,這不是遞歸。
 * Math.max(Math.max(a,b),c);
 * 
 * public void show(int n) {
 * 		if(n <= 0) {
 * 			System.exit(0);
 * 		}
 * 		System.out.println(n);
 * 		show(--n);
 * }
 * 
 * 注意事項:
 * 		A:遞歸一定要有出口,否則就是死遞歸
 * 		B:遞歸的次數不能太多,否則就內存溢出
 * 		C:構造方法不能遞歸使用
 * 
 * 舉例:
 * 		A:從前有座山,山裏有座廟,廟裏有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
 * 			從前有座山,山裏有座廟,廟裏有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
 * 				從前有座山,山裏有座廟,廟裏有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
 * 					從前有座山,山裏有座廟,廟裏有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
 * 						...
 * 					廟掛了,或者山崩了
 * 		B:學編程  -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
 * 			 學編程  -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
 * 				學編程  -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
 * 					學編程  -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
 * 						...
 * 					娶不到媳婦或者生不了娃娃	
 */
public class DiGuiDemo {
	// public DiGuiDemo() {
	// DiGuiDemo();
	// }
}

 

遞歸解決問題的思想及內存圖

遞歸解決問題的思想

•    找到出口

•    找到規律

求階乘案例

•    普通for循環

•    遞歸實現

•    畫內存圖

•    斷點查看執行流程

/*
 * 需求:請用代碼實現求5的階乘。
 * 下面的知識要知道:
 * 		5! = 1*2*3*4*5
 * 		5! = 5*4!
 * 
 * 有幾種方案實現呢?
 * 		A:循環實現
 * 		B:遞歸實現
 * 			a:做遞歸要寫一個方法
 * 			b:出口條件
 * 			c:規律
 */
public class DiGuiDemo {
	public static void main(String[] args) {
		int jc = 1;
		for (int x = 2; x <= 5; x++) {
			jc *= x;
		}
		System.out.println("5的階乘是:" + jc);
		
		System.out.println("5的階乘是:"+jieCheng(5));
	}
	
	/*
	 * 做遞歸要寫一個方法:
	 * 		返回值類型:int
	 * 		參數列表:int n
	 * 出口條件:
	 * 		if(n == 1) {return 1;}
	 * 規律:
	 * 		if(n != 1) {return n*方法名(n-1);}
	 */
	public static int jieCheng(int n){
		if(n==1){
			return 1;
		}else {
			return n*jieCheng(n-1);
		}
	}
}

 

遞歸練習

兔子問題(斐波那契數列)

/*
 * 有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問第二十個月的兔子對數爲多少?
 * 分析:我們要想辦法找規律
 * 			兔子對數
 * 第一個月: 	1
 * 第二個月:	1
 * 第三個月:	2
 * 第四個月:	3	
 * 第五個月:	5
 * 第六個月:	8
 * ...
 * 
 * 由此可見兔子對象的數據是:
 * 		1,1,2,3,5,8...
 * 規則:
 * 		A:從第三項開始,每一項是前兩項之和
 * 		B:而且說明前兩項是已知的
 * 
 * 如何實現這個程序呢?
 * 		A:數組實現
 * 		B:變量的變化實現
 * 		C:遞歸實現
 * 
 * 假如相鄰的兩個月的兔子對數是a,b
 * 第一個相鄰的數據:a=1,b=1
 * 第二個相鄰的數據:a=1,b=2
 * 第三個相鄰的數據:a=2,b=3
 * 第四個相鄰的數據:a=3,b=5
 * 看到了:下一次的a是以前的b,下一次是以前的a+b	
 */
public class DiGuiDemo2 {
	public static void main(String[] args) {
		// 定義一個數組
		int[] arr = new int[20];
		arr[0] = 1;
		arr[1] = 1;
		// arr[2] = arr[0] + arr[1];
		// arr[3] = arr[1] + arr[2];
		// ...
		for (int x = 2; x < arr.length; x++) {
			arr[x] = arr[x - 2] + arr[x - 1];
		}
		System.out.println(arr[19]);// 6765
		System.out.println("----------------");

		int a = 1;
		int b = 1;
		for (int x = 0; x < 18; x++) {
			// 臨時變量存儲上一次的a
			int temp = a;
			a = b;
			b = temp + b;
		}
		System.out.println(b);
		System.out.println("----------------");

		System.out.println(fib(20));
	}

	/*
	 * 方法: 返回值類型:int 參數列表:int n 出口條件: 第一個月是1,第二個月是1 規律: 從第三個月開始,每一個月是前兩個月之和
	 */
	public static int fib(int n) {
		if (n == 1 || n == 2) {
			return 1;
		} else {
			return fib(n - 1) + fib(n - 2);
		}
	}
}

遞歸遍歷目錄下指定後綴名結尾的文件名稱

import java.io.File;

/*
 * 需求:請大家把E:\JavaSE目錄下所有的java結尾的文件的絕對路徑給輸出在控制檯。
 * 
 * 分析:
 * 		A:封裝目錄
 * 		B:獲取該目錄下所有的文件或者文件夾的File數組
 * 		C:遍歷該File數組,得到每一個File對象
 * 		D:判斷該File對象是否是文件夾
 * 			是:回到B
 * 			否:繼續判斷是否以.java結尾
 * 				是:就輸出該文件的絕對路徑
 * 				否:不搭理它
 */
public class FilePathDemo {
	public static void main(String[] args) {
		// 封裝目錄
		File srcFolder = new File("E:\\JavaSE");

		// 遞歸功能實現
		getAllJavaFilePaths(srcFolder);
	}

	private static void getAllJavaFilePaths(File srcFolder) {
		// 獲取該目錄下所有的文件或者文件夾的File數組
		File[] fileArray = srcFolder.listFiles();

		// 遍歷該File數組,得到每一個File對象
		for (File file : fileArray) {
			// 判斷該File對象是否是文件夾
			if (file.isDirectory()) {
				getAllJavaFilePaths(file);
			} else {
				// 繼續判斷是否以.java結尾
				if (file.getName().endsWith(".java")) {
					// 就輸出該文件的絕對路徑
					System.out.println(file.getAbsolutePath());
				}
			}
		}
	}
}

遞歸刪除帶內容的目錄

import java.io.File;

/*
 * 需求:遞歸刪除帶內容的目錄
 * 
 * 目錄我已經給定:demo
 * 
 * 分析:
 * 		A:封裝目錄
 * 		B:獲取該目錄下的所有文件或者文件夾的File數組
 * 		C:遍歷該File數組,得到每一個File對象
 * 		D:判斷該File對象是否是文件夾
 * 			是:回到B
 * 			否:就刪除
 */
public class FileDeleteDemo {
	public static void main(String[] args) {
		// 封裝目錄
		File srcFolder = new File("demo");
		// 遞歸實現
		deleteFolder(srcFolder);
	}

	private static void deleteFolder(File srcFolder) {
		// 獲取該目錄下的所有文件或者文件夾的File數組
		File[] fileArray = srcFolder.listFiles();

		if (fileArray != null) {
			// 遍歷該File數組,得到每一個File對象
			for (File file : fileArray) {
				// 判斷該File對象是否是文件夾
				if (file.isDirectory()) {
					deleteFolder(file);
				} else {
					System.out.println(file.getName() + "---" + file.delete());
				}
			}

			System.out
					.println(srcFolder.getName() + "---" + srcFolder.delete());
		}
	}
}

四、IO流

IO流用來處理設備之間的數據傳輸

•     上傳文件和下載文件

Java對數據的操作是通過流的方式

Java用於操作流的對象都在IO包中

IO流分類

按照數據流向

•    輸入流     讀入數據

•    輸出流     寫出數據

按照數據類型

•    字節流

•    字符流

•    什麼情況下使用哪種流呢?

•   如果數據所在的文件通過windows自帶的記事本打開並能讀懂裏面的內容,就用字符流。其他用字節流。

•   如果你什麼都不知道,就用字節流

 

IO流常用基類

字節流的抽象基類:

•     InputStream OutputStream

字符流的抽象基類:

•     Reader Writer

注:由這四個類派生出來的子類名稱都是以其父類名作爲子類名的後綴。

•     如:InputStream的子類FileInputStream

•     如:Reader的子類FileReader

 

字節流寫數據

OutputStream

•    FileOutputStream

往一個文本文件中寫一句話:helloworld

•    分析發現其實更適合用字符流,但是由於字節流先出現,所以,我們考慮先使用字節流後面再講會什麼出現字符流。

FileOutputStream的構造方法

•    FileOutputStream(Filefile)

•    FileOutputStream(Stringname)

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

/*
 * IO流的分類:
 * 		流向:
 * 			輸入流	讀取數據
 * 			輸出流 寫出數據
 * 		數據類型:
 * 			字節流
 * 				字節輸入流	讀取數據	InputStream
 * 				字節輸出流	寫出數據	OutputStream
 * 			字符流
 * 				字符輸入流	讀取數據	Reader
 * 				字符輸出流	寫出數據	Writer
 * 
 * 		注意:一般我們在探討IO流的時候,如果沒有明確說明按哪種分類來說,默認情況下是按照數據類型來分的。
 * 
 * 需求:我要往一個文本文件中輸入一句話:"hello,io"
 * 
 * 分析:
 * 		A:這個操作最好是採用字符流來做,但是呢,字符流是在字節流之後纔出現的,所以,今天我先講解字節流如何操作。
 * 		B:由於我是要往文件中寫一句話,所以我們要採用字節輸出流。
 * 
 * 通過上面的分析後我們知道要使用:OutputStream
 * 但是通過查看API,我們發現該流對象是一個抽象類,不能實例化。
 * 所以,我們要找一個具體的子類。
 * 而我們要找的子類是什麼名字的呢?這個時候,很簡單,我們回想一下,我們是不是要往文件中寫東西。
 * 文件是哪個單詞:File
 * 然後用的是字節輸出流,聯起來就是:FileOutputStream
 * 注意:每種基類的子類都是以父類名作爲後綴名。
 * 		XxxOutputStream
 * 		XxxInputStream
 * 		XxxReader
 * 		XxxWriter
 * 查看FileOutputStream的構造方法:
 * 		FileOutputStream(File file) 
 *		FileOutputStream(String name)
 *
 * 字節輸出流操作步驟:
 * 		A:創建字節輸出流對象
 * 		B:寫數據
 * 		C:釋放資源
 */
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("java".getBytes());
		
		//釋放資源
		//關閉此文件輸出流並釋放與此流有關的所有系統資源。
		fos.close();
		/*
		 * 爲什麼一定要close()呢?
		 * A:讓流對象變成垃圾,這樣就可以被垃圾回收器回收了
		 * B:通知系統去釋放跟該文件相關的資源
		 */
		//java.io.IOException: Stream Closed
		//fos.write("java".getBytes());
	}
}

 

字節流寫數據的方式

public void write(int b)

public void write(byte[]b)

public void write(byte[]b,int off,int len)

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

/*
 * 字節輸出流操作步驟:
 * A:創建字節輸出流對象
 * B:調用write()方法
 * C:釋放資源
 * 
 * public void write(int b):寫一個字節
 * public void write(byte[] b):寫一個字節數組
 * public void write(byte[] b,int off,int len):寫一個字節數組的一部分
 */
public class FileOutputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 創建字節輸出流對象
		// OutputStream os = new FileOutputStream("fos2.txt"); // 多態
		FileOutputStream fos = new FileOutputStream("fos2.txt");

		// 調用write()方法
		//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();
	}
}

 

字節流寫數據常見問題

創建字節輸出流到底做了哪些事情?

數據寫成功後,爲什麼要close()?

如何實現數據的換行?

如何實現數據的追加寫入?

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

/*
 * 如何實現數據的換行?
 * 		爲什麼現在沒有換行呢?因爲你值寫了字節數據,並沒有寫入換行符號。
 * 		如何實現呢?寫入換行符號即可唄。
 * 		剛纔我們看到了有寫文本文件打開是可以的,通過windows自帶的那個不行,爲什麼呢?
 * 		因爲不同的系統針對不同的換行符號識別是不一樣的?
 * 		windows:\r\n
 * 		linux:\n
 * 		Mac:\r
 * 		而一些常見的個高級記事本,是可以識別任意換行符號的。
 * 
 * 如何實現數據的追加寫入?
 * 		用構造方法帶第二個參數是true的情況即可
 */
public class FileOutputStreamDemo3 {
	public static void main(String[] args) throws IOException {
		// 創建字節輸出流對象
		// FileOutputStream fos = new FileOutputStream("fos3.txt");
		// 創建一個向具有指定 name 的文件中寫入數據的輸出文件流。如果第二個參數爲 true,則將字節寫入文件末尾處,而不是寫入文件開始處。
		FileOutputStream fos = new FileOutputStream("fos3.txt", true);

		// 寫數據
		for (int x = 0; x < 10; x++) {
			fos.write(("hello" + x).getBytes());
			fos.write("\r\n".getBytes());
		}

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

 

字節流寫數據加入異常處理

加入異常處理的IO流操作

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();
				}
			}
		}
	}
}

 

字節流讀取數據

InputStream

•    FileInputStream

把剛纔寫的數據讀取出來顯示在控制檯

FileInputStream的構造方法

•    FileInputStream(Filefile)

•    FileInputStream(Stringname)

FileInputStream的成員方法

•    public int read()

•    public int read(byte[]b)

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

/*
 * 字節輸入流操作步驟:
 * A:創建字節輸入流對象
 * B:調用read()方法讀取數據,並把數據顯示在控制檯
 * C:釋放資源
 * 
 * 讀取數據的方式:
 * A:int read():一次讀取一個字節
 * B:int read(byte[] b):一次讀取一個字節數組
 */
public class FileInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// FileInputStream(String name)
		// FileInputStream fis = new FileInputStream("fis.txt");
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// // 調用read()方法讀取數據,並把數據顯示在控制檯
		// // 第一次讀取
		// int by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		//
		// // 第二次讀取
		// by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		//
		// // 第三次讀取
		// by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		// // 我們發現代碼的重複度很高,所以我們要用循環改進
		// // 而用循環,最麻煩的事情是如何控制循環判斷條件呢?
		// // 第四次讀取
		// by = fis.read();
		// System.out.println(by);
		// // 第五次讀取
		// by = fis.read();
		// System.out.println(by);
		// //通過測試,我們知道如果你讀取的數據是-1,就說明已經讀取到文件的末尾了

		// 用循環改進
		// int by = fis.read();
		// while (by != -1) {
		// System.out.print((char) by);
		// by = fis.read();
		// }

		// 最終版代碼
		int by = 0;
		// 讀取,賦值,判斷
		while ((by = fis.read()) != -1) {
			System.out.print((char) by);
		}

		// 釋放資源
		fis.close();
	}
}
import java.io.FileInputStream;
import java.io.IOException;

/*
 * 一次讀取一個字節數組:int read(byte[] b)
 * 返回值其實是實際讀取的字節個數。
 */
public class FileInputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 創建字節輸入流對象
		// FileInputStream fis = new FileInputStream("fis2.txt");
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// 讀取數據
		// 定義一個字節數組
		// 第一次讀取
		// byte[] bys = new byte[5];
		// int len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第二次讀取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第三次讀取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第四次讀取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		// // 代碼重複了,用循環改進
		// // 但是,我不知道結束條件
		// // len = fis.read(bys);
		// // System.out.println(len);
		// // len = fis.read(bys);
		// // System.out.println(len);
		// // 如果讀取到的實際長度是-1,就說明沒有數據了

		// byte[] bys = new byte[115]; // 0
		// int len = 0;
		// while ((len = fis.read(bys)) != -1) {
		// System.out.print(new String(bys, 0, len));
		// // System.out.print(new String(bys)); //千萬要帶上len的使用
		// }

		// 最終版代碼
		// 數組的長度一般是1024或者1024的整數倍
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字節流讀取數據兩種方式圖解比較

一次讀取一個字節

一次讀取一個字節數組

•    每次可以讀取多個數據,提高了操作效率

 

字節流複製數據練習

把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt

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

/*
 * 複製文本文件。
 * 
 * 數據源:從哪裏來
 * a.txt	--	讀取數據	--	FileInputStream	
 * 
 * 目的地:到哪裏去
 * b.txt	--	寫數據		--	FileOutputStream
 * 
 * java.io.FileNotFoundException: a.txt (系統找不到指定的文件。)
 * 
 * 這一次複製中文沒有出現任何問題,爲什麼呢?
 * 上一次我們出現問題的原因在於我們每次獲取到一個字節數據,就把該字節數據轉換爲了字符數據,然後輸出到控制檯。
 * 而這一次呢?確實通過IO流讀取數據,寫到文本文件,你讀取一個字節,我就寫入一個字節,你沒有做任何的轉換。
 * 它會自己做轉換。
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileInputStream fis = new FileInputStream("a.txt");
		// 封裝目的地
		FileOutputStream fos = new FileOutputStream("b.txt");

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 釋放資源(先關誰都行)
		fos.close();
		fis.close();
	}
}
import java.util.Arrays;

/*
 * 計算機是如何識別什麼時候該把兩個字節轉換爲一箇中文呢?
 * 在計算機中中文的存儲分兩個字節:
 * 		第一個字節肯定是負數。
 * 		第二個字節常見的是負數,可能有正數。但是沒影響。
 */
public class StringDemo {
	public static void main(String[] args) {
		// String s = "abcde";
		// // [97, 98, 99, 100, 101]

		String s = "我愛你中國";
		// [-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]

		byte[] bys = s.getBytes();
		System.out.println(Arrays.toString(bys));
	}
}

c:\\a.txt內容複製到d:\\b.txt

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

/*
 * 需求:把c:\\a.txt內容複製到d:\\b.txt中
 * 
 * 數據源:
 * 		c:\\a.txt	--	讀取數據	--	FileInputStream
 * 目的地:
 * 		d:\\b.txt	--	寫出數據	--	FileOutputStream
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileInputStream fis = new FileInputStream("c:\\a.txt");
		FileOutputStream fos = new FileOutputStream("d:\\b.txt");

		// 複製數據
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

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

e:\\林青霞.jpg內容複製到當前項目目錄下的mn.jpg

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

/*
 * 需求:把e:\\林青霞.jpg內容複製到當前項目目錄下的mn.jpg中
 * 
 * 數據源:
 * 		e:\\林青霞.jpg	--讀取數據--FileInputStream
 * 目的地:
 * 		mn.jpg--寫出數據--FileOutputStream
 */
public class CopyImageDemo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileInputStream fis = new FileInputStream("e:\\林青霞.jpg");
		// 封裝目的地
		FileOutputStream fos = new FileOutputStream("mn.jpg");

		// 複製數據
		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

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

e:\\哥有老婆.mp4複製到當前項目目錄下的copy.mp4

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

/*
 * 需求:把e:\\哥有老婆.mp4複製到當前項目目錄下的copy.mp4中
 * 
 * 數據源:
 * 		e:\\哥有老婆.mp4--讀取數據--FileInputStream
 * 目的地:
 * 		copy.mp4--寫出數據--FileOutputStream
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
		// 封裝目的地
		FileOutputStream fos = new FileOutputStream("copy.mp4");

		// 複製數據
		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 釋放資源
		fos.close();
		fis.close();
	}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4複製到當前項目目錄下的copy.mp4中
 * 
 * 數據源:
 * 		e:\\哥有老婆.mp4--讀取數據--FileInputStream
 * 目的地:
 * 		copy.mp4--寫出數據--FileOutputStream
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
		// 封裝目的地
		FileOutputStream fos = new FileOutputStream("copy.mp4");

		// 複製數據
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

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

 

字節緩衝流

字節流一次讀寫一個數組的速度明顯比一次讀寫一個字節的速度快很多,這是加入了數組這樣的緩衝區效果,java本身在設計的時候,也考慮到了這樣的設計思想(裝飾設計模式後面講解),所以提供了字節緩衝區流

字節緩衝輸出流

•    BufferedOutputStream

字節緩衝輸入流

•    BufferedInputStream

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

/*
 * 通過定義數組的方式確實比以前一次讀取一個字節的方式快很多,所以,看來有一個緩衝區還是非常好的。
 * 既然是這樣的話,那麼,java開始在設計的時候,它也考慮到了這個問題,就專門提供了帶緩衝區的字節類。
 * 這種類被稱爲:緩衝區類(高效類)
 * 寫數據:BufferedOutputStream
 * 讀數據:BufferedInputStream
 * 
 * 構造方法可以指定緩衝區的大小,但是我們一般用不上,因爲默認緩衝區大小就足夠了。
 * 
 * 爲什麼不傳遞一個具體的文件或者文件路徑,而是傳遞一個OutputStream對象呢?
 * 原因很簡單,字節緩衝區流僅僅提供緩衝區,爲高效而設計的。但是呢,真正的讀寫操作還得靠基本的流對象實現。
 */
public class BufferedOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		// BufferedOutputStream(OutputStream out)
		// FileOutputStream fos = new FileOutputStream("bos.txt");
		// BufferedOutputStream bos = new BufferedOutputStream(fos);
		// 簡單寫法
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("bos.txt"));

		// 寫數據
		bos.write("hello".getBytes());

		// 釋放資源
		bos.close();
	}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
 * 注意:雖然我們有兩種方式可以讀取,但是,請注意,這兩種方式針對同一個對象在一個代碼中只能使用一個。
 */
public class BufferedInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// BufferedInputStream(InputStream in)
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				"bos.txt"));

		// 讀取數據
		// int by = 0;
		// while ((by = bis.read()) != -1) {
		// System.out.print((char) by);
		// }
		// System.out.println("---------");

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字節緩衝流複製數據練習

四種方式比較複製效率

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4複製到當前項目目錄下的copy.mp4中
 * 
 * 字節流四種方式複製文件:
 * 基本字節流一次讀寫一個字節:	共耗時:117235毫秒
 * 基本字節流一次讀寫一個字節數組: 共耗時:156毫秒
 * 高效字節流一次讀寫一個字節: 共耗時:1141毫秒
 * 高效字節流一次讀寫一個字節數組: 共耗時:47毫秒
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		// method1("e:\\哥有老婆.mp4", "copy1.mp4");
		// method2("e:\\哥有老婆.mp4", "copy2.mp4");
		// method3("e:\\哥有老婆.mp4", "copy3.mp4");
		method4("e:\\哥有老婆.mp4", "copy4.mp4");
		long end = System.currentTimeMillis();
		System.out.println("共耗時:" + (end - start) + "毫秒");
	}

	// 高效字節流一次讀寫一個字節數組:
	public static void method4(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		bis.close();
	}

	// 高效字節流一次讀寫一個字節:
	public static void method3(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		int by = 0;
		while ((by = bis.read()) != -1) {
			bos.write(by);

		}

		bos.close();
		bis.close();
	}

	// 基本字節流一次讀寫一個字節數組
	public static void method2(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		fos.close();
		fis.close();
	}

	// 基本字節流一次讀寫一個字節
	public static void method1(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fos.close();
		fis.close();
	}
}

五、轉換流

轉換流出現的原因及思想

由於字節流操作中文不是特別方便,所以,java就提供了轉換流。

字符流=字節流+編碼表。

 

編碼表概述和常見的編碼表

編碼表

•    由字符及其對應的數值組成的一張表

常見編碼表

•    ASCII/Unicode 字符集

•    ISO-8859-1

•    GB2312/GBK/GB18030

•    BIG5

•    UTF-8

 

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

/*
 * 字節流讀取中文可能出現的小問題:
 */
public class FileInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// 創建字節輸入流對象
		FileInputStream fis = new FileInputStream("a.txt");

		// 讀取數據
		// int by = 0;
		// while ((by = fis.read()) != -1) {
		// System.out.print((char) by);
		// }

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字符串中的編碼問題

編碼

•    把看得懂的變成看不懂的

解碼

•    把看不懂的變成看得懂的

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/*
 * String(byte[] bytes, String charsetName):通過指定的字符集解碼字節數組
 * byte[] getBytes(String charsetName):使用指定的字符集合把字符串編碼爲字節數組
 * 
 * 編碼:把看得懂的變成看不懂的
 * String -- byte[]
 * 
 * 解碼:把看不懂的變成看得懂的
 * byte[] -- String
 * 
 * 舉例:諜戰片(發電報,接電報)
 * 
 * 碼錶:小本子
 * 		字符	數值
 * 
 * 要發送一段文字:
 * 		今天晚上在老地方見
 * 
 * 		發送端:今 -- 數值 -- 二進制 -- 發出去
 * 		接收端:接收 -- 二進制 -- 十進制 -- 數值 -- 字符 -- 今
 * 
 * 		今天晚上在老地方見
 * 
 * 編碼問題簡單,只要編碼解碼的格式是一致的。
 */
public class StringDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String s = "你好";

		// String -- byte[]
		byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
		// byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
		// byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
		System.out.println(Arrays.toString(bys));

		// byte[] -- String
		String ss = new String(bys); // 你好
		// String ss = new String(bys, "GBK"); // 你好
		// String ss = new String(bys, "UTF-8"); // ???
		System.out.println(ss);
	}
}

 

轉換流概述

OutputStreamWriter字符輸出流

•    public OutputStreamWriter(OutputStream out)

•    public OutputStreamWriter(OutputStream out,String charsetName)

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

/*
 * OutputStreamWriter(OutputStream out):根據默認編碼把字節流的數據轉換爲字符流
 * OutputStreamWriter(OutputStream out,String charsetName):根據指定編碼把字節流數據轉換爲字符流
 * 把字節流轉換爲字符流。
 * 字符流 = 字節流 +編碼表。
 */
public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException {
		// 創建對象
		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
		// "osw.txt")); // 默認GBK
		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
		// "osw.txt"), "GBK"); // 指定GBK
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"osw.txt"), "UTF-8"); // 指定UTF-8
		// 寫數據
		osw.write("中國");

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

InputStreamReader字符輸入流

•    public InputStreamReader(InputStream in)

•    public InputStreamReader(InputStream in,String charsetName)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * InputStreamReader(InputStream is):用默認的編碼讀取數據
 * InputStreamReader(InputStream is,String charsetName):用指定的編碼讀取數據
 */
public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException {
		// 創建對象
		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
		// "osw.txt"));

		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
		// "osw.txt"), "GBK");

		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"osw.txt"), "UTF-8");

		// 讀取數據
		// 一次讀取一個字符
		int ch = 0;
		while ((ch = isr.read()) != -1) {
			System.out.print((char) ch);
		}

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

 

OutputStreamWriter寫數據

OutputStreamWriter寫數據方法

•    public void write(int c)

•    public void write(char[] cbuf)

•    public void write(char[] cbuf,int off,int len)

•    public void write(String str)

•    public void write(String str,int off,int len)

字符流操作要注意的問題

•    flush()的作用

•    flush()close()的區別

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

/*
 * OutputStreamWriter的方法:
 * public void write(int c):寫一個字符
 * public void write(char[] cbuf):寫一個字符數組
 * public void write(char[] cbuf,int off,int len):寫一個字符數組的一部分
 * public void write(String str):寫一個字符串
 * public void write(String str,int off,int len):寫一個字符串的一部分
 * 
 * 面試題:close()和flush()的區別?
 * A:close()關閉流對象,但是先刷新一次緩衝區。關閉之後,流對象不可以繼續再使用了。
 * B:flush()僅僅刷新緩衝區,刷新之後,流對象還可以繼續使用。
 */
public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException {
		// 創建對象
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"osw2.txt"));

		// 寫數據
		// public void write(int c):寫一個字符
		// osw.write('a');
		// osw.write(97);
		// 爲什麼數據沒有進去呢?
		// 原因是:字符 = 2字節
		// 文件中數據存儲的基本單位是字節。
		// void flush()

		// public void write(char[] cbuf):寫一個字符數組
		// char[] chs = {'a','b','c','d','e'};
		// osw.write(chs);

		// public void write(char[] cbuf,int off,int len):寫一個字符數組的一部分
		// osw.write(chs,1,3);

		// public void write(String str):寫一個字符串
		// osw.write("我愛林青霞");

		// public void write(String str,int off,int len):寫一個字符串的一部分
		osw.write("我愛林青霞", 2, 3);

		// 刷新緩衝區
		osw.flush();
		// osw.write("我愛林青霞", 2, 3);

		// 釋放資源
		osw.close();
		// java.io.IOException: Stream closed
		// osw.write("我愛林青霞", 2, 3);
	}
}

 

InputStreamReader讀數據

OutputStreamWriter讀數據方法

•    public int read()

•    public int read(char[] cbuf)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * InputStreamReader的方法:
 * int read():一次讀取一個字符
 * int read(char[] chs):一次讀取一個字符數組
 */
public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException {
		// 創建對象
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"StringDemo.java"));

		// 一次讀取一個字符
		// int ch = 0;
		// while ((ch = isr.read()) != -1) {
		// System.out.print((char) ch);
		// }

		// 一次讀取一個字符數組
		char[] chs = new char[1024];
		int len = 0;
		while ((len = isr.read(chs)) != -1) {
			System.out.print(new String(chs, 0, len));
		}

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

 

字符流複製文本文件

把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*
 * 需求:把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt中
 * 
 * 數據源:
 * 		a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader
 * 目的地:
 * 		b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"a.txt"));
		// 封裝目的地
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"b.txt"));

		// 讀寫數據
		// 方式1
		// int ch = 0;
		// while ((ch = isr.read()) != -1) {
		// osw.write(ch);
		// }

		// 方式2
		char[] chs = new char[1024];
		int len = 0;
		while ((len = isr.read(chs)) != -1) {
			osw.write(chs, 0, len);
			// osw.flush();
		}

		// 釋放資源
		osw.close();
		isr.close();
	}
}

 

轉換流的簡化寫法

轉換流的名字比較長,而我們常見的操作都是按照本地默認編碼實現的,所以,爲了簡化我們的書寫,轉換流提供了對應的子類。

FileWriter

FileReader

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 由於我們常見的操作都是使用本地默認編碼,所以,不用指定編碼。
 * 而轉換流的名稱有點長,所以,Java就提供了其子類供我們使用。
 * OutputStreamWriter = FileOutputStream + 編碼表(GBK)
 * FileWriter = FileOutputStream + 編碼表(GBK)
 * 
 * InputStreamReader = FileInputStream + 編碼表(GBK)
 * FileReader = FileInputStream + 編碼表(GBK)
 * 
 /*
 * 需求:把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt中
 * 
 * 數據源:
 * 		a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader -- FileReader
 * 目的地:
 * 		b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter -- FileWriter
 */
public class CopyFileDemo2 {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileReader fr = new FileReader("a.txt");
		// 封裝目的地
		FileWriter fw = new FileWriter("b.txt");

		// 一次一個字符
		// int ch = 0;
		// while ((ch = fr.read()) != -1) {
		// fw.write(ch);
		// }

		// 一次一個字符數組
		char[] chs = new char[1024];
		int len = 0;
		while ((len = fr.read(chs)) != -1) {
			fw.write(chs, 0, len);
			fw.flush();
		}

		// 釋放資源
		fw.close();
		fr.close();
	}
}

 

FileWriterFileReader

FileWriter寫數據

FileReader讀取數據

FileWriterFileReader實現文本文件的複製

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把c:\\a.txt內容複製到d:\\b.txt中
 * 
 * 數據源:
 * 		c:\\a.txt -- FileReader
 * 目的地:
 * 		d:\\b.txt -- FileWriter
 */
public class CopyFileDemo3 {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		FileReader fr = new FileReader("c:\\a.txt");
		// 封裝目的地
		FileWriter fw = new FileWriter("d:\\b.txt");

		// 讀寫數據
		// int ch = 0;
		int ch;
		while ((ch = fr.read()) != -1) {
			fw.write(ch);
		}
		
		//釋放資源
		fw.close();
		fr.close();
	}
}

字符緩衝流

BufferedWriter基本用法

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 字符流爲了高效讀寫,也提供了對應的字符緩衝流。
 * BufferedWriter:字符緩衝輸出流
 * BufferedReader:字符緩衝輸入流
 * 
 * BufferedWriter:字符緩衝輸出流
 * 將文本寫入字符輸出流,緩衝各個字符,從而提供單個字符、數組和字符串的高效寫入。 
 * 可以指定緩衝區的大小,或者接受默認的大小。在大多數情況下,默認值就足夠大了。 
 */
public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
		// BufferedWriter(Writer out)
		// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
		// new FileOutputStream("bw.txt")));

		BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

		bw.write("hello");
		bw.write("world");
		bw.write("java");
		bw.flush();

		bw.close();
	}
}

BufferedReader基本用法

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/*
 * BufferedReader
 * 從字符輸入流中讀取文本,緩衝各個字符,從而實現字符、數組和行的高效讀取。 
 * 可以指定緩衝區的大小,或者可使用默認的大小。大多數情況下,默認值就足夠大了。 
 * 
 * BufferedReader(Reader in)
 */
public class BufferedReaderDemo {
	public static void main(String[] args) throws IOException {
		// 創建字符緩衝輸入流對象
		BufferedReader br = new BufferedReader(new FileReader("bw.txt"));

		// 方式1
		// int ch = 0;
		// while ((ch = br.read()) != -1) {
		// System.out.print((char) ch);
		// }

		// 方式2
		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			System.out.print(new String(chs, 0, len));
		}

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

字符緩衝流複製文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt中
 * 
 * 數據源:
 * 		a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader -- FileReader -- BufferedReader
 * 目的地:
 * 		b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		// 封裝目的地
		BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

		// 兩種方式其中的一種一次讀寫一個字符數組
		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			bw.write(chs, 0, len);
			bw.flush();
		}

		// 釋放資源
		bw.close();
		br.close();
	}
}

特殊功能

•    BufferedWriter

•   void newLine()

•    BufferedReader

•   String readLine()

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 字符緩衝流的特殊方法:
 * BufferedWriter:
 * 		public void newLine():根據系統來決定換行符
 * BufferedReader:
 * 		public String readLine():一次讀取一行數據
 * 		包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
 */
public class BufferedDemo {
	public static void main(String[] args) throws IOException {
		// write();
		read();
	}

	private static void read() throws IOException {
		// 創建字符緩衝輸入流對象
		BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));

		// public String readLine():一次讀取一行數據
		// String line = br.readLine();
		// System.out.println(line);
		// line = br.readLine();
		// System.out.println(line);

		// 最終版代碼
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		
		//釋放資源
		br.close();
	}

	private static void write() throws IOException {
		// 創建字符緩衝輸出流對象
		BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
		for (int x = 0; x < 10; x++) {
			bw.write("hello" + x);
			// bw.write("\r\n");
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}

}

字符緩衝流特殊功能複製文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把當前項目目錄下的a.txt內容複製到當前項目目錄下的b.txt中
 * 
 * 數據源:
 * 		a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader -- FileReader -- BufferedReader
 * 目的地:
 * 		b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
 */
public class CopyFileDemo2 {
	public static void main(String[] args) throws IOException {
		// 封裝數據源
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		// 封裝目的地
		BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

		// 讀寫數據
		String line = null;
		while ((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
			bw.flush();
		}

		// 釋放資源
		bw.close();
		br.close();
	}
}

 

IO流小結

|--字節流

|--字節輸入流

  • InputStream

  • int read():一次讀取一個字節

  • int read(byte[] bys):一次讀取一個字節數組

  • |--FileInputStream

  • |--BufferedInputStream


|--字節輸出流

  • OutputStream

  • void write(int by):一次寫一個字節

  • void write(byte[] bys,int index,int len):一次寫一個字節數組的一部分

  • |--FileOutputStream

  • |--BufferedOutputStream


|--字符流
|--字符輸入流

  • Reader

  • int read():一次讀取一個字符

  • int read(char[] chs):一次讀取一個字符數組

  • |--InputStreamReader

  • |--FileReader

  • |--BufferedReader

  • String readLine():一次讀取一個字符串


|--字符輸出流

  • Writer

  • void write(int ch):一次寫一個字符

  • void write(char[] chs,int index,int len):一次寫一個字符數組的一部分

  • |--OutputStreamWriter

  • |--FileWriter

  • |--BufferedWriter

  • void newLine():寫一個換行符

  • void write(String line):一次寫一個字符串

 

IO流案例

l  複製文本文件

l  複製圖片

l  ArrayList集合中的字符串數據存儲到文本文件

l  從文本文件中讀取數據(每一行爲一個字符串數據)到集合中,並遍歷集合

l  複製單極文件夾

l  複製單極文件夾中指定文件並修改文件名稱

l  複製多極文件夾

l  鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件

 

IO流練習

l  已知s.txt文件中有這樣的一個字符串:“hcexfgijkamdnoqrzstuvwybpl

 請編寫程序讀取數據內容,把數據排序後寫入ss.txt中。

l  Reader模擬BufferedReaderreadLine()功能

l  自定義類模擬LineNumberReader的特有功能

•   獲取每次讀取數據的行號

l  登錄註冊IO

 


七、其他流

操作基本數據類型的流

操作基本數據類型

•    DataInputStream

•    DataOutputStream

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 可以讀寫基本數據類型的數據
 * 數據輸入流:DataInputStream
 * 			DataInputStream(InputStream in)
 * 數據輸出流:DataOutputStream
 * 			DataOutputStream(OutputStream out) 
 */
public class DataStreamDemo {
	public static void main(String[] args) throws IOException {
		// 寫
		// write();

		// 讀
		read();
	}

	private static void read() throws IOException {
		// DataInputStream(InputStream in)
		// 創建數據輸入流對象
		DataInputStream dis = new DataInputStream(
				new FileInputStream("dos.txt"));

		// 讀數據
		byte b = dis.readByte();
		short s = dis.readShort();
		int i = dis.readInt();
		long l = dis.readLong();
		float f = dis.readFloat();
		double d = dis.readDouble();
		char c = dis.readChar();
		boolean bb = dis.readBoolean();

		// 釋放資源
		dis.close();

		System.out.println(b);
		System.out.println(s);
		System.out.println(i);
		System.out.println(l);
		System.out.println(f);
		System.out.println(d);
		System.out.println(c);
		System.out.println(bb);
	}

	private static void write() throws IOException {
		// DataOutputStream(OutputStream out)
		// 創建數據輸出流對象
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				"dos.txt"));

		// 寫數據了
		dos.writeByte(10);
		dos.writeShort(100);
		dos.writeInt(1000);
		dos.writeLong(10000);
		dos.writeFloat(12.34F);
		dos.writeDouble(12.56);
		dos.writeChar('a');
		dos.writeBoolean(true);

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

 

內存操作流

操作字節數組

•    ByteArrayInputStream

•    ByteArrayOutputStream

操作字符數組

•    CharArrayReader

•    CharArrayWrite

操作字符串

•    StringReader

•    StringWriter

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/*
 * 內存操作流:用於處理臨時存儲信息的,程序結束,數據就從內存中消失。
 * 字節數組:
 * 		ByteArrayInputStream
 * 		ByteArrayOutputStream
 * 字符數組:
 * 		CharArrayReader
 * 		CharArrayWriter
 * 字符串:
 * 		StringReader
 * 		StringWriter
 */
public class ByteArrayStreamDemo {
	public static void main(String[] args) throws IOException {
		// 寫數據
		// ByteArrayOutputStream()
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 寫數據
		for (int x = 0; x < 10; x++) {
			baos.write(("hello" + x).getBytes());
		}

		// 釋放資源
		// 通過查看源碼我們知道這裏什麼都沒做,所以根本需要close()
		// baos.close();

		// public byte[] toByteArray()
		byte[] bys = baos.toByteArray();

		// 讀數據
		// ByteArrayInputStream(byte[] buf)
		ByteArrayInputStream bais = new ByteArrayInputStream(bys);

		int by = 0;
		while ((by = bais.read()) != -1) {
			System.out.print((char) by);
		}

		// bais.close();
	}
}

 

打印流

打印流概述

•    字節流打印流

•    字符打印流

打印流特點

•    只能操作目的地,不能操作數據。

•    可以操作任意類型的數據。

•    如果啓動了自動刷新,能夠自動刷新。

•    可以操作文件的流

import java.io.IOException;
import java.io.PrintWriter;

/*
 * 打印流
 * 字節流打印流	PrintStream
 * 字符打印流	PrintWriter
 * 
 * 打印流的特點:
 * 		A:只有寫數據的,沒有讀取數據。只能操作目的地,不能操作數據源。
 * 		B:可以操作任意類型的數據。
 * 		C:如果啓動了自動刷新,能夠自動刷新。
 * 		D:該流是可以直接操作文本文件的。
 * 			哪些流對象是可以直接操作文本文件的呢?
 * 			FileInputStream
 * 			FileOutputStream
 * 			FileReader
 * 			FileWriter
 * 			PrintStream
 * 			PrintWriter
 * 			看API,查流對象的構造方法,如果同時有File類型和String類型的參數,一般來說就是可以直接操作文件的。
 * 
 * 			流:
 * 				基本流:就是能夠直接讀寫文件的
 * 				高級流:在基本流基礎上提供了一些其他的功能
 */
public class PrintWriterDemo {
	public static void main(String[] args) throws IOException {
		// 作爲Writer的子類使用
		PrintWriter pw = new PrintWriter("pw.txt");

		pw.write("hello");
		pw.write("world");
		pw.write("java");
		
		pw.close();
	}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * 1:可以操作任意類型的數據。
 * 		print()
 * 		println()
 * 2:啓動自動刷新
 * 		PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);
 * 		還是應該調用println()的方法纔可以
 * 		這個時候不僅僅自動刷新了,還實現了數據的換行。
 * 
 * 		println()
 *		其實等價于于:
 *		bw.write();
 *		bw.newLine();		
 *		bw.flush();
 */
public class PrintWriterDemo2 {
	public static void main(String[] args) throws IOException {
		// 創建打印流對象
		// PrintWriter pw = new PrintWriter("pw2.txt");
		PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);

		// write()是搞不定的,怎麼辦呢?
		// 我們就應該看看它的新方法
		// pw.print(true);
		// pw.print(100);
		// pw.print("hello");

		pw.println("hello");
		pw.println(true);
		pw.println(100);

		pw.close();
	}
}

l 打印流複製文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * 需求:DataStreamDemo.java複製到Copy.java中
 * 數據源:
 * 		DataStreamDemo.java -- 讀取數據 -- FileReader -- BufferedReader
 * 目的地:
 * 		Copy.java -- 寫出數據 -- FileWriter -- BufferedWriter -- PrintWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 以前的版本
		// 封裝數據源
		// BufferedReader br = new BufferedReader(new FileReader(
		// "DataStreamDemo.java"));
		// // 封裝目的地
		// BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
		//
		// String line = null;
		// while ((line = br.readLine()) != null) {
		// bw.write(line);
		// bw.newLine();
		// bw.flush();
		// }
		//
		// bw.close();
		// br.close();

		// 打印流的改進版
		// 封裝數據源
		BufferedReader br = new BufferedReader(new FileReader(
				"DataStreamDemo.java"));
		// 封裝目的地
		PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);
		
		String line = null;
		while((line=br.readLine())!=null){
			pw.println(line);
		}
		
		pw.close();
		br.close();
	}
}

 

標準輸入輸出流

System類中的字段:inout

它們各代表了系統標準的輸入和輸出設備。

默認輸入設備是鍵盤,輸出設備是顯示器。

System.in的類型是InputStream.

System.out的類型是PrintStreamOutputStream的子類FilterOutputStream 的子類.

import java.io.PrintStream;

/*
 * 標準輸入輸出流
 * System類中的兩個成員變量:
 *		public static final InputStream in “標準”輸入流。
 * 		public static final PrintStream out “標準”輸出流。
 * 
 * 		InputStream is = System.in;
 * 		PrintStream ps = System.out;
 */
public class SystemOutDemo {
	public static void main(String[] args) {
		// 有這裏的講解我們就知道了,這個輸出語句其本質是IO流操作,把數據輸出到控制檯。
		System.out.println("helloworld");

		// 獲取標準輸出流對象
		PrintStream ps = System.out;
		ps.println("helloworld");
		
		ps.println();
		// ps.print();//這個方法不存在
		
		// System.out.println();
		// System.out.print();
	}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/*
 * 轉換流的應用。
 */
public class SystemOutDemo2 {
	public static void main(String[] args) throws IOException {
		// 獲取標準輸入流
		// // PrintStream ps = System.out;
		// // OutputStream os = ps;
		// OutputStream os = System.out; // 多態
		// // 我能不能按照剛纔使用標準輸入流的方式一樣把數據輸出到控制檯呢?
		// OutputStreamWriter osw = new OutputStreamWriter(os);
		// BufferedWriter bw = new BufferedWriter(osw);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				System.out));

		bw.write("hello");
		bw.newLine();
		// bw.flush();
		bw.write("world");
		bw.newLine();
		// bw.flush();
		bw.write("java");
		bw.newLine();
		bw.flush();
		
		bw.close();
	}
}

 

隨機訪問流

RandomAccessFile概述

•    RandomAccessFile類不屬於流,是Object類的子類。但它融合了InputStreamOutputStream的功能。支持對隨機訪問文件的讀取和寫入。

案例演示

import java.io.IOException;
import java.io.RandomAccessFile;

/*
 * 隨機訪問流:
 * 		RandomAccessFile類不屬於流,是Object類的子類。
 * 		但它融合了InputStream和OutputStream的功能。
 * 		支持對文件的隨機訪問讀取和寫入。
 * 
 * public RandomAccessFile(String name,String mode):第一個參數是文件路徑,第二個參數是操作文件的模式。
 * 		模式有四種,我們最常用的一種叫"rw",這種方式表示我既可以寫數據,也可以讀取數據 
 */
public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		// write();
		read();
	}

	private static void read() throws IOException {
		// 創建隨機訪問流對象
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");

		int i = raf.readInt();
		System.out.println(i);
		// 該文件指針可以通過 getFilePointer方法讀取,並通過 seek 方法設置。
		System.out.println("當前文件的指針位置是:" + raf.getFilePointer());

		char ch = raf.readChar();
		System.out.println(ch);
		System.out.println("當前文件的指針位置是:" + raf.getFilePointer());

		String s = raf.readUTF();
		System.out.println(s);
		System.out.println("當前文件的指針位置是:" + raf.getFilePointer());

		// 我不想重頭開始了,我就要讀取a,怎麼辦呢?
		raf.seek(4);
		ch = raf.readChar();
		System.out.println(ch);
	}

	private static void write() throws IOException {
		// 創建隨機訪問流對象
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");

		// 怎麼玩呢?
		raf.writeInt(100);
		raf.writeChar('a');
		raf.writeUTF("中國");

		raf.close();
	}
}

 

合併流

SequenceInputStream概述

•    SequenceInputStream類可以將多個輸入流串流在一起,合併爲一個輸入流,因此,該流也被稱爲合併流。

SequenceInputStream的構造方法

•    SequenceInputStream(InputStreams1, InputStream s2) 

•    SequenceInputStream(Enumeration<?extends InputStream> e)

•    把多個文件的內容寫入到一個文本文件

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;

/*
 * 以前的操作:
 * a.txt -- b.txt
 * c.txt -- d.txt
 * 
 * 現在想要:
 * a.txt+b.txt -- c.txt
 */
public class SequenceInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// SequenceInputStream(InputStream s1, InputStream s2)
		// 需求:把ByteArrayStreamDemo.java和DataStreamDemo.java的內容複製到Copy.java中
		InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
		InputStream s2 = new FileInputStream("DataStreamDemo.java");
		SequenceInputStream sis = new SequenceInputStream(s1, s2);
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("Copy.java"));

		// 如何寫讀寫呢,其實很簡單,你就按照以前怎麼讀寫,現在還是怎麼讀寫
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = sis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		sis.close();
	}
}
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

/*
 * 以前的操作:
 * a.txt -- b.txt
 * c.txt -- d.txt
 * e.txt -- f.txt
 * 
 * 現在想要:
 * a.txt+b.txt+c.txt -- d.txt
 */
public class SequenceInputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 需求:把下面的三個文件的內容複製到Copy.java中
		// ByteArrayStreamDemo.java,CopyFileDemo.java,DataStreamDemo.java

		// SequenceInputStream(Enumeration e)
		// 通過簡單的回顧我們知道了Enumeration是Vector中的一個方法的返回值類型。
		// Enumeration<E> elements()
		Vector<InputStream> v = new Vector<InputStream>();
		InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
		InputStream s2 = new FileInputStream("CopyFileDemo.java");
		InputStream s3 = new FileInputStream("DataStreamDemo.java");
		v.add(s1);
		v.add(s2);
		v.add(s3);
		Enumeration<InputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("Copy.java"));

		// 如何寫讀寫呢,其實很簡單,你就按照以前怎麼讀寫,現在還是怎麼讀寫
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = sis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		sis.close();
	}
}

 

序列化流

序列化流

•    ObjectOutputStream

反序列化流

•    ObjectInputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
 * 序列化流:把對象按照流一樣的方式存入文本文件或者在網絡中傳輸。對象 -- 流數據(ObjectOutputStream)
 * 反序列化流:把文本文件中的流對象數據或者網絡中的流對象數據還原成對象。流數據 -- 對象(ObjectInputStream)
 */
public class ObjectStreamDemo {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// 由於我們要對對象進行序列化,所以我們先自定義一個類
		// 序列化數據其實就是把對象寫到文本文件
		// write();

		read();
	}

	private static void read() throws IOException, ClassNotFoundException {
		// 創建反序列化對象
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"oos.txt"));

		// 還原對象
		Object obj = ois.readObject();

		// 釋放資源
		ois.close();

		// 輸出對象
		System.out.println(obj);
	}

	private static void write() throws IOException {
		// 創建序列化流對象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"oos.txt"));

		// 創建對象
		Person p = new Person("林青霞", 27);

		// public final void writeObject(Object obj)
		oos.writeObject(p);

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

序列化操作問題

•    爲什麼要實現序列化?

•    如何實現序列化?

•    序列化數據後,再次修改類文件,讀取數據會出問題,如何解決呢?

•    使用transient關鍵字聲明不需要序列化的成員變量

import java.io.Serializable;

/*
 * NotSerializableException:未序列化異常
 * 
 * 類通過實現 java.io.Serializable 接口以啓用其序列化功能。未實現此接口的類將無法使其任何狀態序列化或反序列化。
 * 該接口居然沒有任何方法,類似於這種沒有方法的接口被稱爲標記接口。
 * 
 * java.io.InvalidClassException: 
 * cn.itcast_07.Person; local class incompatible: 
 * stream classdesc serialVersionUID = -2071565876962058344, 
 * local class serialVersionUID = -8345153069362641443
 * 
 * 爲什麼會有問題呢?
 * 		Person類實現了序列化接口,那麼它本身也應該有一個標記值。
 * 		這個標記值假設是100。
 * 		開始的時候:
 * 		Person.class -- id=100
 * 		wirte數據: oos.txt -- id=100
 * 		read數據: oos.txt -- id=100	
 * 
 * 		現在:
 * 		Person.class -- id=200
 * 		wirte數據: oos.txt -- id=100
 * 		read數據: oos.txt -- id=100
 * 我們在實際開發中,可能還需要使用以前寫過的數據,不能重新寫入。怎麼辦呢?
 * 回想一下原因是因爲它們的id值不匹配。
 * 每次修改java文件的內容的時候,class文件的id值都會發生改變。
 * 而讀取文件的時候,會和class文件中的id值進行匹配。所以,就會出問題。
 * 但是呢,如果我有辦法,讓這個id值在java文件中是一個固定的值,這樣,你修改文件的時候,這個id值還會發生改變嗎?
 * 不會。現在的關鍵是我如何能夠知道這個id值如何表示的呢?
 * 不用擔心,你不用記住,也沒關係,點擊鼠標即可。
 * 你難道沒有看到黃色警告線嗎?
 * 
 * 我們要知道的是:
 * 		看到類實現了序列化接口的時候,要想解決黃色警告線問題,就可以自動產生一個序列化id值。
 * 		而且產生這個值以後,我們對類進行任何改動,它讀取以前的數據是沒有問題的。
 * 
 * 注意:
 * 		我一個類中可能有很多的成員變量,有些我不想進行序列化。請問該怎麼辦呢?
 * 		使用transient關鍵字聲明不需要序列化的成員變量
 */
public class Person implements Serializable {
	private static final long serialVersionUID = -2071565876962058344L;

	private String name;

	// private int age;

	private transient int age;

	// int age;

	public Person() {
		super();
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

 

Properties集合

Properties概述

Properties作爲Map集合的使用

import java.util.Properties;
import java.util.Set;

/*
 * Properties:屬性集合類。是一個可以和IO流相結合使用的集合類。
 * Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。 
 * 
 * 是Hashtable的子類,說明是一個Map集合。
 */
public class PropertiesDemo {
	public static void main(String[] args) {
		// 作爲Map集合的使用
		// 下面這種用法是錯誤的,一定要看API,如果沒有<>,就說明該類不是一個泛型類,在使用的時候就不能加泛型
		// Properties<String, String> prop = new Properties<String, String>();

		Properties prop = new Properties();

		// 添加元素
		prop.put("it002", "hello");
		prop.put("it001", "world");
		prop.put("it003", "java");

		// System.out.println("prop:" + prop);

		// 遍歷集合
		Set<Object> set = prop.keySet();
		for (Object key : set) {
			Object value = prop.get(key);
			System.out.println(key + "---" + value);
		}
	}
}

Properties的特殊功能

•    public ObjectsetProperty(String key,String value)

•    public StringgetProperty(String key)

•    public Set<String>stringPropertyNames()

import java.util.Properties;
import java.util.Set;

/*
 * 特殊功能:
 * public Object setProperty(String key,String value):添加元素
 * public String getProperty(String key):獲取元素
 * public Set<String> stringPropertyNames():獲取所有的鍵的集合
 */
public class PropertiesDemo2 {
	public static void main(String[] args) {
		// 創建集合對象
		Properties prop = new Properties();

		// 添加元素
		prop.setProperty("張三", "30");
		prop.setProperty("李四", "40");
		prop.setProperty("王五", "50");

		// public Set<String> stringPropertyNames():獲取所有的鍵的集合
		Set<String> set = prop.stringPropertyNames();
		for (String key : set) {
			String value = prop.getProperty(key);
			System.out.println(key + "---" + value);
		}
	}
}

/*
 * class Hashtalbe<K,V> { public V put(K key,V value) { ... } }
 * 
 * class Properties extends Hashtable { public V setProperty(String key,String
 * value) { return put(key,value); } }
 */

PropertiesIO流的結合使用

  • 把鍵值對形式的文本文件內容加載到集合中

  • public void load(Reader reader)

  • public void load(InputStream inStream)

  • 把集合中的數據存儲到文本文件中

  • public void store(Writer writer,String comments)

  • public void store(OutputStream out,String comments)

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

/*
 * 這裏的集合必須是Properties集合:
 * public void load(Reader reader):把文件中的數據讀取到集合中
 * public void store(Writer writer,String comments):把集合中的數據存儲到文件
 * 
 * 單機版遊戲:
 * 		進度保存和加載。
 * 		三國羣英傳,三國志,仙劍奇俠傳...
 * 
 * 		呂布=1
 * 		方天畫戟=1
 */
public class PropertiesDemo3 {
	public static void main(String[] args) throws IOException {
		// myLoad();

		myStore();
	}

	private static void myStore() throws IOException {
		// 創建集合對象
		Properties prop = new Properties();

		prop.setProperty("林青霞", "27");
		prop.setProperty("武鑫", "30");
		prop.setProperty("劉曉曲", "18");
		
		//public void store(Writer writer,String comments):把集合中的數據存儲到文件
		Writer w = new FileWriter("name.txt");
		prop.store(w, "helloworld");
		w.close();
	}

	private static void myLoad() throws IOException {
		Properties prop = new Properties();

		// public void load(Reader reader):把文件中的數據讀取到集合中
		// 注意:這個文件的數據必須是鍵值對形式
		Reader r = new FileReader("prop.txt");
		prop.load(r);
		r.close();

		System.out.println("prop:" + prop);
	}
}

|  案例1

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import java.util.Set;

/*
 * 我有一個文本文件(user.txt),我知道數據是鍵值對形式的,但是不知道內容是什麼。
 * 請寫一個程序判斷是否有“lisi”這樣的鍵存在,如果有就改變其實爲”100”
 * 
 * 分析:
 * 		A:把文件中的數據加載到集合中
 * 		B:遍歷集合,獲取得到每一個鍵
 * 		C:判斷鍵是否有爲"lisi"的,如果有就修改其值爲"100"
 * 		D:把集合中的數據重新存儲到文件中
 */
public class PropertiesTest {
	public static void main(String[] args) throws IOException {
		// 把文件中的數據加載到集合中
		Properties prop = new Properties();
		Reader r = new FileReader("user.txt");
		prop.load(r);
		r.close();

		// 遍歷集合,獲取得到每一個鍵
		Set<String> set = prop.stringPropertyNames();
		for (String key : set) {
			// 判斷鍵是否有爲"lisi"的,如果有就修改其值爲"100"
			if ("lisi".equals(key)) {
				prop.setProperty(key, "100");
				break;
			}
		}

		// 把集合中的數據重新存儲到文件中
		Writer w = new FileWriter("user.txt");
		prop.store(w, null);
		w.close();
	}
}

|  案例2

import java.util.Scanner;

/**
 * 這是猜數字小遊戲
 */
public class GuessNumber {
	private GuessNumber() {
	}

	public static void start() {
		// 產生一個隨機數
		int number = (int) (Math.random() * 100) + 1;

		// 定義一個統計變量
		int count = 0;

		while (true) {
			// 鍵盤錄入一個數據
			Scanner sc = new Scanner(System.in);
			System.out.println("請輸入數據(1-100):");
			int guessNumber = sc.nextInt();

			count++;

			// 判斷
			if (guessNumber > number) {
				System.out.println("你猜的數據" + guessNumber + "大了");
			} else if (guessNumber < number) {
				System.out.println("你猜的數據" + guessNumber + "小了");
			} else {
				System.out.println("恭喜你," + count + "次就猜中了");
				break;
			}
		}
	}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

/*
 * 我有一個猜數字小遊戲的程序,請寫一個程序實現在測試類中只能用5次,超過5次提示:遊戲試玩已結束,請付費。
 */
public class PropertiesTest2 {
	public static void main(String[] args) throws IOException {
		// 讀取某個地方的數據,如果次數不大於5,可以繼續玩。否則就提示"遊戲試玩已結束,請付費。"
		// 創建一個文件
		// File file = new File("count.txt");
		// if (!file.exists()) {
		// file.createNewFile();
		// }

		// 把數據加載到集合中
		Properties prop = new Properties();
		Reader r = new FileReader("count.txt");
		prop.load(r);
		r.close();

		// 我自己的程序,我當然知道里面的鍵是誰
		String value = prop.getProperty("count");
		int number = Integer.parseInt(value);

		if (number > 5) {
			System.out.println("遊戲試玩已結束,請付費。");
			System.exit(0);
		} else {
			number++;
			prop.setProperty("count", String.valueOf(number));
			Writer w = new FileWriter("count.txt");
			prop.store(w, null);
			w.close();

			GuessNumber.start();
		}
	}
}

 

NIO包下的IO

NIO其實就是新IO的意思。

•    JDK4出現NIO。新IO和傳統的IO有相同的目的,都是用於進行輸入輸出的,但新IO使用了不同的方式來處理輸入輸出,採用內存映射文件的方式,將文件或者文件的一段區域映射到內存中,就可以像訪問內存一樣的來訪問文件了,這種方式效率比舊IO要高很多,但是目前好多地方我們看到的還是舊IO的引用,所以我們仍以舊IO爲主,知道NIO即可。

•    JDK7IO改進(寫一個案例)

•   Path

•   Paths

•   Files

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

/*
 * nio包在JDK4出現,提供了IO流的操作效率。但是目前還不是大範圍的使用。
 * 有空的話瞭解下,有問題再問我。
 * 
 * JDK7的之後的nio:
 * Path:路徑
 * Paths:有一個靜態方法返回一個路徑
 * 		public static Path get(URI uri)
 * Files:提供了靜態方法供我們使用
 * 		public static long copy(Path source,OutputStream out):複製文件
 * 		public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs,OpenOption... options)
 */
public class NIODemo {
	public static void main(String[] args) throws IOException {
		// public static long copy(Path source,OutputStream out)
		// Files.copy(Paths.get("ByteArrayStreamDemo.java"), new
		// FileOutputStream(
		// "Copy.java"));

		ArrayList<String> array = new ArrayList<String>();
		array.add("hello");
		array.add("world");
		array.add("java");
		Files.write(Paths.get("array.txt"), array, Charset.forName("GBK"));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章