/*
* I/O流技術(shù) 字符流
* 字節(jié)流: 字節(jié)流讀取的以二進(jìn)制的形式讀取數(shù)據(jù)
* 字符流:字符流會把讀取到的二進(jìn)制數(shù)進(jìn)行對飲的編碼和解碼工作,字符流=字節(jié)流+編碼?解碼
*
* 輸入字符流體系:
* ---| Reader 輸入字符流的基類,是一個抽象類
* -------|FileReader 讀取文件的輸入字符流
*
* FileReader使用步驟:
* 1. 定位目標(biāo)文件:
* 2.構(gòu)建字符流的輸入通道;
* 3.讀取數(shù)據(jù)
* 4.關(guān)閉資源
*/
package com.michael.lin;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) throws IOException{
readDate();
readData2();
}
//使用緩沖數(shù)組讀取文件內(nèi)容
public static void readData2() throws IOException{
//1.定位目標(biāo)文件
File file = new File("c:\\data.txt");
//2.構(gòu)建緩沖數(shù)組和字符流輸入通道
char[] buf = new char[1024];
FileReader fileReader = new FileReader(file);
//3.讀取數(shù)據(jù)
int length = 0;
while((length=fileReader.read(buf))!=-1){
System.out.println("內(nèi)如是:" + new String(buf, 0, buf.length));
}
//4.關(guān)閉資源
fileReader.close();
}
//每次一個字符
public static void readDate() throws IOException{
//1.定位目標(biāo)文件:
File file = new File("c:\\data.txt");
//2,構(gòu)建讀取字符流的輸入通道:
FileReader fileReader = new FileReader(file);
//3.讀取數(shù)據(jù)
int content = 0;
while((content=fileReader.read())!=-1){
System.out.println((char)content);
}
fileReader.close();
}
}