java——常見IO流的使用——FileReader類
阿新 • • 發佈:2020-12-27
一、大綱
1、流的分類
- 根據操作資料單位:位元組流、字元流
- 根據資料流向:輸入流、輸出流
- 根據流的角色:節點流和處理流
2. 流的體系結構(只說重要的流)
抽象基類 | 節點流(檔案流) | 緩衝流(處理流的一種) |
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedoutputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
怎麼從流的類名判斷出是什麼型別的流?
- Input和Reader代表輸入流 ,Output和Writer代表輸出流
- 有File代表節點流,無File代表處理流
- 有Stream代表位元組流,無Stream代表字元流
二、 FileReader類
——作用就是將檔案裡的文字內容讀取到控制檯中
import java.io.File; import java.io.FileReader; import java.io.IOException; public class HTIO { public static void main(String[] args) throws IOException{ //例項化對應的File類,指明要操作的檔案 File file1 = new File("C:\\Users\\Administrator\\Desktop\\hello1.txt"); //提供具體的流 FileReader fr = new FileReader(file1); //資料的讀入 int data; while ((data = fr.read()) != -1){ System.out.print((char) data); } //hello //4.關閉流(注意一定要關閉,否則有可能造成記憶體洩漏) fr.close(); } }
- 滑鼠放在read()上兩秒後會出現此方法的說明:返回的是一個int,即字元對應的ASCI碼,而且是每執行一次fr.read(),指標偏移一次進而返回一個字元的ASCI碼,在sout的時候強轉為(char)。當read()返回-1時代表檔案已經讀完。
- 如果不在 public static void main(String[] args) 上 throws IOException,new FileReader(file1)、fr.read()、fr.close()都會讓你丟擲異常,讓程式看著比較混亂
- 會丟擲異常的原因是有可能檔案不存在
- 但是這種做法的缺點是:在fr.read()處阻塞,丟擲異常,直接停止程式,導致close()無法執行,則建立流之後不能關閉流
- 下面是用try-catch-finally改進優化
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HTIO {
public static void main(String[] args){
FileReader fr = null;
try {
//1.例項化對應的File類,指明要操作的檔案
File file1 = new File("C:\\Users\\Administrator\\Desktop\\hello1.txt");
//2.提供具體的流
fr = new FileReader(file1);
//3.資料的讀入
int data;
while ((data = fr.read()) != -1){
System.out.print((char) data);
}
//hello
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流(注意一定要關閉,否則有可能造成記憶體洩漏)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 選中要try的程式碼塊,ctrl alt t,選中try catch finally
- 把close語句方法finally中,即不管前面的操作出現什麼異常,finally的語句一定會執行
- 舉一個經典的例子來說明try-catch-finally:一個人去上廁所,中間可能遇到拉稀、掉廁所等等異常,然後用catch中去解決,但是最後都要擦屁股,也就是finally
try{一個人去上廁所}
catch{解決措施}
finally{擦屁股}
讀檔案總流程:
1.例項化File類 2.例項化流的類 3.讀入操作 4.關閉流
三、read(char[] cbuf)方法詳解
- 從中可以看到read()方法中可以傳入的引數
- 常用的FileReader.read()的兩個過載方法:1.空參 2.引數:char[] cbuf
- read(char[] cbuf)原理:每次執行此方法會讀取五個字元到char[] cbuf中,下次執行時會讀取接下來的五個字元。返回值是本次讀取的字元個數,讀取到最後會返回-1。
- 需要注意:下面這個程式,倒數第二次陣列中讀取到的是 [ython], 最後一次讀取的時候因為只剩一個'c',覆蓋cbuf的第一個位置,變成 [cthon]
桌面上hello1.txt檔案中的內容:helloworldjavapythonc,任務:讀取檔案內容
錯誤寫法:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HTIO {
public static void main(String[] args){
FileReader fileReader = null;
try {
File file = new File("C:\\Users\\Administrator\\Desktop\\hello1.txt");
fileReader = new FileReader(file);
char[] cbuf = new char[5];
int num;
while ((num=fileReader.read(cbuf))!=-1) {
for(int i=0;i<cbuf.length;i++){
System.out.print(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//helloworldjavapythoncthon
}
}
- 錯誤原因:倒數第二次陣列中讀取到的是 [ython], 最後一次讀取的時候因為只剩一個'c',覆蓋cbuf的第一個位置,變成 [cthon]
正確寫法:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HTIO {
public static void main(String[] args){
FileReader fileReader = null;
try {
File file = new File("C:\\Users\\Administrator\\Desktop\\hello1.txt");
fileReader = new FileReader(file);
char[] cbuf = new char[5];
int num;
while ((num=fileReader.read(cbuf))!=-1) {
for(int i=0;i<num;i++){
System.out.print(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//helloworldjavapythonc
}
}