天天看點

InputStreamReader讀資料的2種方式

public class ReaderDemo {
	public static void main(String[] args) throws IOException {
		// 建立字元輸入流
		InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
		
		// public int read():一次讀取一個字元
//		int ch;
//		while((ch = isr.read()) != -1){
//			System.out.print((char)ch);
//		}
		
		// public int read(char[] cbuf):一次讀取一個字元數組
		char[] chs = new char[1024];
		int len;// 每次真實讀到資料的個數
		while((len = isr.read(chs)) != -1){
			 System.out.print(new String(chs,0,len));
		}
		
		isr.close();
		
	}

}