IO流的分類
按操作的數(shù)據(jù)單位:字節(jié)流(8bit)冈在,字符流(16bit)---->對于文本文件使用字符流處理倒慧,對于非文本文件,使用字節(jié)流處理
按數(shù)據(jù)的流向:輸入流包券,輸出流
按流的角色:節(jié)點流(文件流)纫谅,處理流
抽象基類
抽象基類 | 節(jié)點流(文件流) | 緩沖流(處理流的一種) |
---|---|---|
InputStream | FileInputStream(read(byte[] buffer) ) |
BufferedInputStream (read(byte[] buffer) ) |
OutputStream | FileOutputStream(write(byte[] buffer,0,len) ) |
BufferedOutputStream (write(byte[] buffer,0,len) /flush() ) |
Reader | FileReader(read(char[] cbuf) ) |
BufferedReader(read(char[] cbuf) /readLine() ) |
Writer | FileWriter(write(char[] cbuf,0,len) ) |
BufferedWriter(write(char[] cbuf,0,len) /flush() ) |
FileReader的使用
說明:
1、read()的理解:返回讀入的一個字符溅固,如果達到文件末尾付秕,返回-1
2、異常的處理侍郭,為了保證流資源一定可以執(zhí)行關(guān)閉操作询吴。需要使用try-catch-finally處理。
3亮元、讀入的文件一定要存在猛计,否則就會報FileNotFoundException
public static void main(String[] args) throws IOException {
FileReader fr = null;
try {
//1、實例化File類的對象爆捞,指明要操作的文件
File file1 = new File("day08\\hello.txt");//在main方法里的相對路徑是相對于當(dāng)前工程javasenior奉瘤,在@Test里是相對當(dāng)前MOdule
//2、提供具體的流
fr = new FileReader(file1);
//3煮甥、數(shù)據(jù)的讀入
//read():返回讀入的一個字符毛好,如果達到文件末尾望艺,返回-1
int data = fr.read();
while(data != -1){
System.out.print((char) data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null)
try {
//4、流的關(guān)閉操作
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 改進的read()用法
//read()的改進用法:每次取出固定長度的char[]肌访,而非之前每次讀一個char
@Test
public void test(){
FileReader fr = null;
try {
File file1 = new File("hello.txt");
fr = new FileReader(file1);
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:錯誤的寫法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);//"hello world!orl":錯誤原因:每次輸出固定長度為5的字符串找默,如果文件字符數(shù)不是5的倍數(shù)就不對
// }
//方式一:正確的寫法
for(int i = 0;i < len;i++){
System.out.print(cbuf[i]);//"hello world!"
}
//方式二:錯誤的寫法
// String str = new String(cbuf);
// System.out.print(str);//"hello world!orl"錯誤原因同上
//方式二:正確的寫法
String str = new String(cbuf,0,len);
System.out.print(str);//"hello world!"
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileWriter的使用
說明
1、輸出操作吼驶,對應(yīng)的File可以不存在惩激,并不會報異常
如果不存在:在寫出的過程中,會自動創(chuàng)建文件蟹演;
如果存在:
--------------->①如果流使用的構(gòu)造器是FileWriter(file,false)/FileWriter(file):對原有文件進行覆蓋
--------------->②如果流使用的構(gòu)造器是FileWriter(file,true):不會對原有文件進行覆蓋风钻,而是在其基礎(chǔ)上繼續(xù)寫入。
@Test
public void test1(){
FileWriter fw = null;
try {
//1酒请、提供File類的對象骡技,指明寫出到的文件
File file1 = new File("hello1.txt");
//2、提供FileWriter的對象羞反,用于數(shù)據(jù)的寫出
fw = new FileWriter(file1);
//3布朦、寫出的操作
fw.write("I am a sb\n");
fw.write("I am a nc");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、昼窗;流資源的關(guān)閉
if(fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用FileReader和FileWriter實現(xiàn)文本文件的復(fù)制
- 注:不能使用字符流處理圖片等字節(jié)流數(shù)據(jù)
@Test
public void test2(){
FileReader fr = null;
FileWriter fw = null;
try {
//1是趴、創(chuàng)建File類的對象,指明讀入和寫出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
//2澄惊、創(chuàng)建輸入流和輸出流的對象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3唆途、數(shù)據(jù)的讀入和寫出操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
// for(int i = 0;i < len;i++){
// fw.write(cbuf[i]);
// }
//用這個重載的write()方法,比上面更簡便一些掸驱。
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4肛搬、關(guān)閉輸入流資源
if(fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
//關(guān)閉輸出流資源
if(fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用FileInputStream和FileOutputStream實現(xiàn)非文本文件(如.jpg)的復(fù)制
package java1;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputOutputStreamTest {
@Test
public void test(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File("學(xué)生證.jpg");
File destFile = new File("學(xué)生證1.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fos != null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}