Java中的節點流(或檔案流):FileReader/FileWriter、FileInputStream / FileOutputStream
阿新 • • 發佈:2020-12-14
技術標籤:B_java高階程式設計# B5_Java中的IO流java
1.FileReader/FileWriter的使用:
1.1 FileReader的使用
說明點:
- read()的理解:返回讀入的一個字元。如果達到檔案末尾,返回-1
- 異常的處理:為了保證流資源一定可以執行關閉操作。需要使用try-catch-finally處理
- 讀入的檔案一定要存在,否則就會報FileNotFoundException。
@Test
public void testFileReader1() {
FileReader fr = null;
try {
//1.File類的例項化
File file = new File("hello.txt");//helloworld123
//2.FileReader流的例項化
fr = new FileReader(file);
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf陣列中的字元的個數。如果達到檔案末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
//方式一:
//錯誤的寫法cbuf是每次從頭開始覆蓋原來的資料,
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);//helloworld123ld
// }
//正確的寫法
// for (int i = 0; i < len; i++) {
// System.out.print(cbuf[i]);
// }
//方式二:
//錯誤的寫法,對應著方式一的錯誤的寫法
// String str = new String(cbuf);
// System.out.print(str);
//正確的寫法
String s = new String(cbuf,0,len);
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源的關閉
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2 FileWriter的使用
從記憶體中寫出資料到硬碟的檔案裡。
說明:
- 輸出操作,對應的File可以不存在的。並不會報異常
- File對應的硬碟中的檔案在,在輸出檔案如果不存出的過程中,會自動建立此檔案。
File對應的硬碟中的檔案如果存在:
如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原檔案的覆蓋
如果流使用的構造器是:FileWriter(file,true):不會對原檔案覆蓋,而是在原檔案基礎上追加內容
FileWriter fw = null;
try {
//1.提供File類的物件,指明寫出到的檔案
File file = new File("hello1.txt");
//2.提供FileWriter的物件,用於資料的寫出
fw = new FileWriter(file);
//3.寫出的操作
fw.write("I hava a dream!\n");
fw.write("You need to hava a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null){
//4.流資源的關閉
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.3 文字檔案的複製:
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
//1.建立File類的物件,指明讀入和寫出的檔案
File file1 = new File("day26_IO流\\hello.txt");
File file2 = new File("day26_IO流\\hello1.txt");
//不能使用字元流來處理圖片等位元組資料
// File file1 = new File("day26_IO流\\燦烈.jpg");
// File file2 = new File("day26_IO流\\燦烈1.jpg");
//2.建立輸入流和輸出流的物件
fr = new FileReader(file1);
fw = new FileWriter(file2);
//3.資料的讀入和寫出操作
char[] cbuf = new char[5];
int len;//記錄每次讀入到cbuf陣列中的字元的個數
while ((len = fr.read(cbuf)) != -1) {
//每次寫出len個字元
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流資源
//方式一:
// try {
// if(fw != null)
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//方式二:
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.FileInputStream / FileOutputStream的使用:
- 對於文字檔案(.txt,.java,.c,.cpp),使用字元流處理
- 對於非文字檔案(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用位元組流處理
- 對於位元組流將文字檔案從一個檔案複製到另一個檔案而不是在控制檯輸出,則該操作是可以的,不會出現亂碼。但字元流即使是複製圖片等非文字檔案也是不被允許的。
public class FileInputOutputStreamTest {
/*
實現對圖片的複製操作
*/
@Test
public void testFileInputOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file1 = new File("燦烈.jpg");
File file2 = new File("燦烈1.jpg");
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//複製的過程
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("複製成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
【注意】