java入門 -- Java I/O(七)輸入字符流 FileReader

/*

* I/O流技術(shù) 字符流

* 字節(jié)流: 字節(jié)流讀取的以二進制的形式讀取數(shù)據(jù)

* 字符流:字符流會把讀取到的二進制數(shù)進行對飲的編碼和解碼工作,字符流=字節(jié)流+編碼?解碼

*

* 輸入字符流體系:

* ---| Reader 輸入字符流的基類,是一個抽象類

* -------|FileReader 讀取文件的輸入字符流

*

* FileReader使用步驟:

* 1. 定位目標文件:

* 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.定位目標文件

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.定位目標文件:

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

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容