java-io流-字符流

選擇了安逸,就選擇了平庸 -2018.10.30

字符流

字符流用於處理用ASCII字符集或Unicode(包含ASCII的國際字符集)表示的文本,可以用字符流來處理的文件有純文本文件,HTML文檔,和java源代碼文件。用於讀寫這些流的類都是Reader和Writer的子類,對於所有的文本輸入,都應使用字符流來處理,而不能直接使用字節流來處理。

字符的繼承關係

字符繼承關係圖

讀寫文件

FileReader是InputStreamReader的子類,常用於從文件中讀取字符流。FileWriter爲OutputStreamWriter的子類,常用於向文件中寫字符流。

文件中讀寫

構造方法

public FileReader(File file)  
public FileReader(String fileName) 

public FileWriter(File file) 
       根據給定的 File 對象構造一個 FileWriter 對象。 
public FileWriter(File file, boolean append) 
       根據給定的 File 對象構造一個 FileWriter 對象,append代表是否爲追加。
public FileWriter(String fileName) 
       根據給定的文件名構造一個 FileWriter 對象。 
public FileWriter(String fileName, boolean append) 
       根據給定的文件名以及指示是否附加寫入數據的 boolean 值來構造 FileWriter 對象。

1、在給定指定文件名或者指定的文件的情況下讀寫數據
2、FileNotFoundException -如果文件不存在,或者它是一個目錄,而不是一個常規文件,抑或因爲其他某些原因而無法打開進行讀取
3、IOException - 如果該文件存在,但它是一個目錄,而不是一個常規文件;或者該文件不存在,但無法創建它;抑或因爲其他某些原因而無法打開它

 private static Reader testReader = null;
    private static Writer testWriter = null;
    private static String readFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo.java";
	private static String writeFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo2.java";
    public static void main(String[] args) throws IOException {
		  File  file  = new File(readFilePath);
	       testReader = new FileReader(file);
          //testReade = new FileReader(filePath);
	       //兩種構造方法都可以
	       testWriter = new FileWriter(writeFilePath);//重寫文件
	       //testWriter = new FileWriter(writeFilePath,true);在文件尾追加數據
	       }

常用方法概述

從類 java.io.InputStreamReader 繼承的方法 
close, getEncoding, read, read, ready 
  從類 java.io.Reader 繼承的方法 
mark, markSupported, read, read, reset, skip 

常用方法與字節操作相同

package testIoChar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class IoCharDemo {
    private static Reader testReader = null;
    private static Writer testWriter = null;
    private static String readFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo.java";
	private static String writeFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo2.java";
    public static void main(String[] args) throws IOException {
		  File  file  = new File(readFilePath);
	       testReader = new FileReader(file);
          //testReade = new FileReader(filePath);
	       //兩種構造方法都可以
	       testWriter = new FileWriter(writeFilePath);
	       char[]ch = new char[(int)file.length()];
	       testReader.read(ch);
	       System.out.println(new String(ch));
	       testWriter.write(new String(ch));
	       testReader.close();
	       testWriter.close();
	       //如果沒有關閉,會發現文件中沒有數據,因爲沒有刷新流,數據只是寫在了流裏
	       //close()方法中調用了flush()。
    }
}

轉換流(InputStreamReader,OutputWriter)

InputStreamReader

  • InputStreamReader 是字節流通向字符流的橋樑:它使用指定的 charset 讀取字節並將其解碼爲字符。它使用的字符集可以由名稱指定或顯式給定,或者可以接受平臺默認的字符集
  • 每次調用 InputStreamReader 中的一個 read() 方法都會導致從底層輸入流讀取一個或多個字節。要啓用從字節到字符的有效轉換,可以提前從底層流讀取更多的字節,使其超過滿足當前讀取操作所需的字節。
  • 爲了達到最高效率,可要考慮在 BufferedReader 內包裝 InputStreamReader。例如:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

方法概述

InputStreamReader(InputStream in) 
          創建一個使用默認字符集的 InputStreamReader。 
InputStreamReader(InputStream in, Charset cs) 
          創建使用給定字符集的 InputStreamReader。 
InputStreamReader(InputStream in, CharsetDecoder dec) 
          創建使用給定字符集解碼器的 InputStreamReader。 
InputStreamReader(InputStream in, String charsetName) 
          創建使用指定字符集的 InputStreamReader。 
          charset的常見編碼
          US-ASCII 7 位 ASCII 字符,也叫作 ISO646-US、Unicode 字符集的基本拉丁塊 
		 ISO-8859-1   ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 
		UTF-8 8 位 UCS 轉換格式 
		UTF-16BE 16 位 UCS 轉換格式,Big Endian(最低地址存放高位字節)字節順序 
		UTF-16LE 16 位 UCS 轉換格式,Little-endian(最高地址存放低位字節)字節順序 
		UTF-16 16 位 UCS 轉換格式,字節順序由可選的字節順序標記來標識 
          
普通方法概述
void close() 
          關閉該流並釋放與之關聯的所有資源。 
 String getEncoding() 
          返回此流使用的字符編碼的名稱。 
 int read() 
          讀取單個字符。 
 int read(char[] cbuf, int offset, int length) 
          將字符讀入數組中的某一部分。 
 boolean ready() 
          判斷此流是否已經準備好用於讀取。 

示例
將一個用UTF-8編碼的文本文件用字節流讀進來,觀察之後,再用字符流轉換該字節流,觀察結果。(新建一個文本文檔,命名爲test.txt,另存爲時,選擇UTF-8格式保存。)

package IODemo;

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

public class InputStreamReaderDemo {
   private static String FilePath ="C:/Users/asus-pc/Desktop/javaProject/Demo/src/IODemo/test.txt";
	private static InputStream iStream =null;
	public static void main(String[] args) {
	 	try {
	 		byte bytes[] =new byte[1024];
			iStream =new FileInputStream(FilePath);
			int data =-1;
		  iStream.read(bytes);
		  System.out.println(new String(bytes));
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

運行結果(可以看出是亂碼)

鍩胯繖鏄竴涓祴璇曟枃浠躲??

用轉換流進行轉換讀寫

package IODemo;

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

public class InputStreamReaderDemo {
   private static String FilePath ="C:/Users/asus-pc/Desktop/javaProject/Demo/src/IODemo/test.txt";
	private static InputStream iStream =null;
	private static InputStreamReader iReader =null;
	public static void main(String[] args) {
	 	try {
	 		char ch[] =new char[1024];
			iStream =new FileInputStream(FilePath);
			iReader =new InputStreamReader(iStream,"UTF-8");
			BufferedReader bufferedReader =new BufferedReader(iReader);
			//此處可以添加緩衝流,也可以不添加
		  String data =null;
			while((data=bufferedReader.readLine())!=null)
			 System.out.println(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
}

運行結果

?這是一個測試文件

前面之所以多出一個?,是因爲保存爲txt文件時,有個字節順序標記,出現在文本文件頭部,Unicode編碼標準中用於標識文件是採用哪種格式的編碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章