主要功能:運用多種方式讀取檔案內容
* <p> 包括按位元組讀取檔案內容、按字元讀取檔案內容、按行讀取檔案內容。
* <ul>
* <li> 以位元組為單位讀取檔案,常用於讀二進位檔案,如圖片、聲音、影像等檔案。
* <li> 以字元為單位讀取檔案,常用於讀文本、數字等類型的檔案。要得到標準輸入的位元組流,
* 然後通過橋接轉換為字元流,再經過緩衝得到帶緩衝的標準輸入資料流。
* <li> 以行為單位讀取檔案,常用於讀面向行的格式檔案。
package com.zf.s10.io;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;/** * @檔案名稱 TextReadFile.java * @功能 運用多種方式讀取檔案內容 * <p>包括按位元組讀取檔案內容、按字元讀取檔案內容、按行讀取檔案內容。 * <ul> * <li> 以位元組為單位讀取檔案,常用於讀二進位檔案,如圖片、聲音、影像等檔案。 * <li> 以字元為單位讀取檔案,常用於讀文本、數字等類型的檔案。要得到標準輸入的位元組流, * 然後通過橋接轉換為字元流,再經過緩衝得到帶緩衝的標準輸入資料流。 * <li> 以行為單位讀取檔案,常用於讀面向行的格式檔案。 * </ul> * </p> * @作者 * @日期 2010-4-28 * @版本 M1.0 * @描述 Java 工具類 */public class TextReadFile {/** * 以位元組為單位讀取檔案,常用於讀二進位檔案,如圖片、聲音、影像等檔案。 * * @param fileName 檔案絕對路徑 */public static void readFileByBytes(String fileName) {File file = new File(fileName);// 建立檔案InputStream in = null;try {System.out.println("①.以位元組為單位讀取檔案內容,一次讀一個位元組:");in = new FileInputStream(file);// 將檔案放入檔案輸入資料流中int tempbyte;while ((tempbyte = in.read()) != -1) { // 一次讀一個位元組,迴圈將內容讀出來System.out.write(tempbyte);}in.close();// 關閉檔案輸入資料流} catch (IOException e) {// 捕獲異常e.printStackTrace();return;}try {System.out.println("②.以位元組為單位讀取檔案內容,一次讀多個位元組:");// 一次讀多個位元組byte[] tempbytes = new byte[100];// 聲明長度為100的位元組數組int byteread = 0;in = new FileInputStream(fileName);// 將檔案放入檔案輸入資料流中TextReadFile.showAvailableBytes(in);// //顯示輸入資料流中還剩的位元組數// 讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數while ((byteread = in.read(tempbytes)) != -1) {// 一次讀多個位元組,迴圈將內容讀出來System.out.write(tempbytes, 0, byteread);}} catch (Exception e1) {// 捕獲異常e1.printStackTrace();} finally {// 內容總執行if (in != null) {try {in.close();// 確保檔案輸入資料流關閉} catch (IOException e1) {e1.printStackTrace();}}}}/** * 以字元為單位讀取檔案,常用於讀文本、數字等類型的檔案。 * * @param fileName 檔案絕對路徑 */public static void readFileByChars(String fileName) {File file = new File(fileName);// 建立檔案Reader read = null;try {System.out.println("①.以字元為單位讀取檔案內容,一次讀一個位元組:");// 一次讀一個字元read = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = read.read()) != -1) {// 對於windows下,rn這兩個字元在一起時,表示一個換行。// 但如果這兩個字元分開顯示時,會換兩次行。// 因此,屏蔽掉r,或者屏蔽n。否則,將會多出很多空行。if (((char) tempchar) != 'r') {// 只要是不換行就讀取System.out.print((char) tempchar);}}read.close();} catch (Exception e) {e.printStackTrace();}try {System.out.println("②.以字元為單位讀取檔案內容,一次讀多個位元組:");char[] tempchars = new char[30];int charread = 0;read = new InputStreamReader(new FileInputStream(fileName));// 建立檔案讀入流while ((charread = read.read(tempchars)) != -1) {// 一次讀多個字元// 同樣屏蔽掉r不顯示if ((charread == tempchars.length)&& (tempchars[tempchars.length - 1] != 'r')) {System.out.print(tempchars);} else {for (int i = 0; i < charread; i++) {if (tempchars[i] == 'r') {continue;// 停止執行當前的迭代,然後退回迴圈開始處} else {System.out.print(tempchars[i]);}}}}} catch (Exception e1) {// 捕獲異常e1.printStackTrace();} finally {// 內容總執行if (read != null) {try {read.close();// 確保關閉} catch (IOException e1) {}}}}/** * 以行為單位讀取檔案,常用於讀面向行的格式檔案。 * * @param fileName 檔案絕對路徑 */public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;// 建立緩衝讀取try {System.out.println("①.以行為單位讀取檔案內容,一次讀一整行:");reader = new BufferedReader(new FileReader(file));// 將檔案放在緩衝讀取中String tempString = null;int line = 1;// 一次讀入一行,直到讀入null為檔案結束while ((tempString = reader.readLine()) != null) {// 顯示行號System.out.println("line " + line + ": " + tempString);line++;}reader.close();} catch (IOException e) {// 捕獲異常e.printStackTrace();} finally {// 內容總執行if (reader != null) {try {reader.close();// 關閉緩衝讀取} catch (IOException e1) {}}}}/** * 顯示輸入資料流中還剩的位元組數 * * @param in */private static void showAvailableBytes(InputStream in) {try {System.out.println("當前位元組輸入資料流中的位元組數為:" + in.available());} catch (IOException e) {// 捕獲異常e.printStackTrace();}}public static void main(String[] args) {String fileName = "E:/poem.txt";System.out.println("1.按位元組為單位讀取檔案:");readFileByBytes(fileName);System.out.println("2.按字元為單位讀取檔案:");readFileByChars(fileName);System.out.println("3.按行為單位讀取檔案:");readFileByLines(fileName);}}