I/O流总结
一,I/O流概述
I/O流簡單的理解就是數(shù)據(jù)的輸入與輸出;那數(shù)據(jù)的的輸入與輸出又怎么理解呢?
首先我們知道,所有的數(shù)據(jù)在計(jì)算機(jī)中都是以二進(jìn)制的形式存儲(chǔ)的.我們看到的字節(jié)或者字符形式的文件都是計(jì)算機(jī)經(jīng)過解析之后形成的.
那么數(shù)據(jù)的輸入與輸出簡單地說,就是我們向計(jì)算機(jī)(通信設(shè)備)存數(shù)據(jù)和取數(shù)據(jù)的方式,這種方式包括兩種,一種是字節(jié)形式的存取,一種是字符形式的存取.
那什么時(shí)候用字節(jié),什么時(shí)候用字符呢?這是我們今天討論的重點(diǎn).
二,I/O流的體系(字符流)
如圖:
?
I/O流的體系非常之龐大.但是我們從體系的頂層向下看的話,其實(shí)還是很好掌握的;
?? ?1)我們從體系圖中可以看出
?? ?I/O流按照輸入和輸出來分就是:
?? ??? ??? ?輸入流:
?? ??? ??? ??? ??? ?字節(jié)輸入流:InputStream
?? ??? ??? ??? ??? ?字符輸入流:Reader
?? ??? ??? ?輸出流:
?? ??? ??? ??? ??? ?字節(jié)輸出流:OutputStream
?? ??? ??? ??? ??? ?字符輸出流:Writer
?? ??? ??? ??? ?
?? ?2)了解的內(nèi)容:
?? ??? ?
?? ??? ??? ?當(dāng)處理純文本內(nèi)容時(shí),建議使用字符流;
?? ??? ??? ?
?? ??? ??? ?當(dāng)處理圖像,音頻文件時(shí),使用字節(jié)流;
?? ??? ??? ?
三,體系解析:
?? ?3.1 字符流操作文本文件:
?? ?
?? ?示例1:向計(jì)算機(jī)硬盤中寫入些文字;
?? ?
?? ?import java.io.FileWriter;
?? ?import java.io.IOException;
?? ?public class FileWriterDemo {
?? ??? ?//因?yàn)榭紤]到不同系統(tǒng)的的換行符號(hào)不一致,所以我們直接調(diào)用系統(tǒng)的換行設(shè)置
?? ??? ?private static final String LINE_SEPARATOR = System.getProperty("line.separator");
?? ??? ?public static void main(String[] args) throws IOException {?? ??? ?
?? ??? ?//注意當(dāng)我這里后面不加true的話,表示多次向文件添加內(nèi)容是,內(nèi)容無法追加,每寫一次都會(huì)把上次的內(nèi)容覆蓋掉;
?? ??? ?FileWriter fw=new FileWriter("E:\\IO測試\\demo.txt",true);
?? ??? ?fw.write("Hello World"+LINE_SEPARATOR+"I LOVE JAVA");
?? ??? ??? ??? ?
?? ??? ?fw.write(LINE_SEPARATOR+"pierce");
?? ??? ?//注意要刷新,否則,無法寫入內(nèi)容(前提是流沒有關(guān)閉)?? ?
?? ??? ?fw.flush();
?? ??? ?//關(guān)閉流通道;
?? ??? ?fw.close();
?? ??? ??? ??? ?
?? ??? ?}
}
?? ?//注意:
?? ??? ??? ?關(guān)閉流,必須先刷新此流,一旦關(guān)閉后,就不能再寫數(shù)據(jù),和調(diào)用flush方法;
?? ??? ??? ?假如,我在fw.close()后面,在寫一個(gè)fw.write("Hello");會(huì)報(bào)錯(cuò);
?? ??? ??? ?
?? ?示例二:從計(jì)算機(jī)指定文件讀取數(shù)據(jù),輸出在控制臺(tái)上;
?? ?
?? ?方式一:一次讀取單個(gè)字符的方式,讀取文件
?? ?
?? ??? ?import java.io.FileReader;
?? ??? ?import java.io.IOException;
?? ?public class FileReaderDemo {
?? ??? ?
?? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ?//創(chuàng)建一個(gè)要讀取的文件對(duì)象
?? ??? ??? ?FileReader fr=new FileReader("E:\\IO測試\\demo.txt");
?? ??? ??? ?//定義一個(gè)變量,用于存儲(chǔ)每次讀到的那個(gè)字符對(duì)應(yīng)的數(shù)字
?? ??? ??? ?int ch=0;
?? ??? ??? ?//每次讀一個(gè),讀到文件末尾時(shí),返回一個(gè)-1,表示文件已經(jīng)讀取完畢;
?? ??? ??? ?while((ch=fr.read())!=-1){
?? ??? ??? ?//將每次讀到的一個(gè)字符輸出
?? ??? ??? ?System.out.print((char)ch);
?? ??? ??? ?}
?? ??? ?//關(guān)閉流通道;
?? ??? ?fr.close();
?? ?}
}
?? ?方式二: 一次讀取多個(gè)字符的方式,讀取文件
?? ?
?? ?import java.io.FileReader;
?? ?import java.io.IOException;
?? ?public class FileReaderDemo2 {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//創(chuàng)建一個(gè)要讀取的文件對(duì)象
?? ??? ?FileReader fr=new FileReader("E:\\IO測試\\demo.txt");
?? ??? ?//創(chuàng)建一個(gè)字符數(shù)組,用于存儲(chǔ)每次讀到的數(shù)據(jù)
?? ??? ?char []ch=new char[1024];//注意這里統(tǒng)一使用計(jì)算機(jī)的千字節(jié)數(shù)的整數(shù)倍
?? ??? ?//讀到文件末尾的標(biāo)記;
?? ??? ?int len=0;
?? ??? ?while((len=fr.read(ch))!=-1){
?? ??? ??? ?//最后一次只需讀取有效數(shù)據(jù)即可
?? ??? ??? ?System.out.println(new String(ch,0,len));
?? ??? ?}
?? ?}
}
?? ?
?? ?方式三,使用高效緩沖流,向文件中寫數(shù)據(jù);
?? ?
?? ?import java.io.BufferedWriter;
?? ?import java.io.FileWriter;
?? ?import java.io.IOException;
?? ?public class BufferWriterDemo {
?? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ?//目標(biāo)對(duì)象
?? ??? ??? ?FileWriter fw=new FileWriter("D:\\demo.txt");
?? ??? ??? ?//
?? ??? ??? ?BufferedWriter bw=new BufferedWriter(fw);
?? ??? ??? ?
?? ??? ??? ?for (int i = 0; i < 4; i++) {
?? ??? ??? ??? ?bw.write("嘻嘻");
?? ??? ??? ??? ?//BufferedWriter的自有換行操作
?? ??? ??? ??? ?bw.newLine();
?? ??? ??? ??? ?
?? ??? ??? ??? ?bw.flush();
?? ??? ??? ?}
?? ??? ??? ?bw.close();
?? ??? ?}
}
?? ?
?? ??? ?方式四:使用高效率流從文件中讀取數(shù)據(jù);
?? ??? ?import java.io.BufferedReader;
?? ??? ?import java.io.FileNotFoundException;
?? ??? ?import java.io.FileReader;
?? ??? ?import java.io.IOException;
?? ?public class BufferReaderDemo {
?? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ?FileReader fr=new FileReader("D:\\demo.txt");
?? ??? ??? ?BufferedReader br=new BufferedReader(fr);?? ?
?? ??? ??? ?//定義一個(gè)變量,用于存儲(chǔ)每次讀到一行數(shù)據(jù);
?? ??? ??? ?String line=null;?? ?
?? ??? ??? ?//當(dāng)讀到文件末尾時(shí),返回null,就結(jié)束讀取操作
?? ??? ??? ?while((line=br.readLine())!=null){
?? ??? ??? ??? ?System.out.println(line);
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?我們已經(jīng)學(xué)會(huì)了上面的五中操作方法,都是單方向的操作文件;下面我們來復(fù)制一個(gè)文件到指定路徑下;
?? ?
?? ?案例:復(fù)制文件操作:
?? ?
?? ?import java.io.BufferedReader;
?? ?import java.io.BufferedWriter;
?? ?import java.io.FileReader;
?? ?import java.io.FileWriter;
?? ?import java.io.IOException;
public class BufferedTest_CopyFile {
?? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ?
?? ??? ??? ?FileReader fr=new FileReader("D:\\demo.txt");
?? ??? ??? ?
?? ??? ??? ?BufferedReader br=new BufferedReader(fr);
?? ??? ??? ?
?? ??? ??? ?FileWriter fw=new FileWriter("E:\\T.txt");
?? ??? ??? ?
?? ??? ??? ?BufferedWriter bw=new BufferedWriter(fw);
?? ??? ??? ?
?? ??? ??? ?String line=null;
?? ??? ??? ?//讀一行寫一行
?? ??? ??? ?while((line=br.readLine())!=null){
?? ??? ??? ??? ?bw.write(line);
?? ??? ??? ??? ?bw.newLine();
?? ??? ??? ??? ?bw.flush();
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?//當(dāng)然,我們也可以一個(gè)字節(jié),一個(gè)字節(jié)的讀,或者每次讀一個(gè)數(shù)組長度的方式去讀取文件;
?? ??? ??? ?/*
?? ??? ??? ?*? 單字符讀取的1方式;
?? ??? ??? ?*?? ?int ch=0;
?? ??? ??? ?*?? ?while((ch=br.read())!=-1){
?? ??? ??? ?*?? ?bw.write(ch);
?? ??? ??? ?*?? ?}
?? ??? ??? ?*/
?? ??? ??? ?//讀
?? ??? ??? ?/*?? ?char []char=new char[1024*4];
?? ??? ??? ?*?? ?int len;
?? ??? ??? ?*?? ?while((len=br.read(char))!=-1){
?? ??? ??? ?*?? ?bw.write(char,0,len);
?? ??? ??? ?*?? ?bw.flush();
?? ??? ??? ?*?? ?}
?? ??? ??? ?*/
?? ??? ??? ?bw.close();
?? ??? ??? ?br.close();
?? ??? ??? ?
?? ?讀取文件時(shí),I/O流異常的處理方式:
?? ?import java.io.FileWriter;
?? ?import java.io.IOException;
?? ?public class IOExceptionDemo {
?? ??? ?private static final String LINE_SEPARATOR = System.getProperty("line.separator");
?? ??? ?public static void main(String[] args) {
?? ??? ??? ?FileWriter fw =null;//在try外面聲明引用型變量,再處理異常時(shí)再初始化變量;
?? ??? ??? ?try {
?? ??? ??? ??? ?fw = new FileWriter("E:\\IO測試\\demo.txt",true);
?? ??? ??? ??? ?
?? ??? ??? ??? ?fw.write("Hello World"+LINE_SEPARATOR+"I LOVE JAVA");
?? ??? ??? ??? ?
?? ??? ??? ??? ?fw.write(LINE_SEPARATOR+"pierce");
?? ??? ??? ??? ?
?? ??? ??? ??? ?fw.flush();?? ??? ?
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}finally{
?? ??? ??? ??? ??? ?if(fw!=null)//如果寫入過程中,出現(xiàn)意外,也要關(guān)閉流通道;
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?fw.close();
?? ??? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ??? ?throw new RuntimeException("關(guān)閉失敗..");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}?? ?
?? ?}
}
?? ??? ??? ?
?? ?//下面我們看下一個(gè)有趣的流:LineNumberReader,它可以拿到每一行文字對(duì)應(yīng)的行號(hào):
??? ?//跟蹤行號(hào)的緩沖字符輸入流
?? ?import java.io.FileReader;
?? ?import java.io.IOException;
?? ?import java.io.LineNumberReader;
?? ?public class LineNumberReaderDemo {
?? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ?FileReader fr=new FileReader("D:\\d.txt");
?? ??? ??? ?
?? ??? ??? ?LineNumberReader inr=new LineNumberReader(fr);
?? ??? ??? ?
?? ??? ??? ?String line=null;
?? ??? ??? ?
?? ??? ??? ?while((line=inr.readLine())!=null){
?? ??? ??? ??? ?
?? ??? ??? ??? ?System.out.println(inr.getLineNumber()+":"+line);
?? ??? ??? ?}
?? ??? ??? ?inr.close();
?? ?}
}
?? ?3.2)轉(zhuǎn)換流操作:
?? ??? ?import java.io.BufferedReader;
?? ??? ?import java.io.BufferedWriter;
?? ??? ?import java.io.IOException;
?? ??? ?import java.io.InputStreamReader;
?? ??? ?import java.io.OutputStreamWriter;
?? ??? ?public class TransStreamDemo2 {
?? ??? ??? ?public static void main(String[] args) throws IOException {
?? ??? ??? ??? ?//字節(jié)流
?? ??? ??? ??? ?//InputStream in=System.in;
?? ??? ??? ??? ?//字節(jié)轉(zhuǎn)換為字符的橋梁,轉(zhuǎn)換流;
?? ??? ??? ??? ?//InputStreamReader isr=new InputStreamReader(in);
?? ??? ??? ??? ?
?? ??? ??? ??? ?//字符流;
?? ??? ??? ??? ?//BufferedReader br=new BufferedReader(isr);
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?//OutputStream out=System.out;
?? ??? ??? ??? ?
?? ??? ??? ??? ?//OutputStreamWriter osw=new OutputStreamWriter(out);
?? ??? ??? ??? ?
?? ??? ??? ??? ?//BufferedWriter bw=new BufferedWriter(osw);
?? ??? ??? ??? ?
?? ??? ??? ??? ?BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
?? ??? ??? ??? ?BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
?? ??? ??? ??? ?//?? ?BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\insert.txt")));?? ?
?? ??? ??? ??? ?String line=null;
?? ??? ??? ??? ?while((line=br.readLine())!=null){?? ??? ??? ?
?? ??? ??? ??? ??? ?if("over".equals(line))?? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?break;?? ??? ?
?? ??? ??? ??? ??? ?bw.write(line.toUpperCase());
?? ??? ??? ??? ??? ?bw.newLine();
?? ??? ??? ??? ??? ?bw.flush();
?? ??? ??? ??? ?}
?? ?}
}
?? ??? ??? ?
?? ??? ??? ?/*
?? ??? ??? ? * 如果操作文本需要明確具體的編碼,必須要轉(zhuǎn)換流
?? ??? ??? ? *
?? ??? ??? ? * */
?? ?import java.io.FileInputStream;
?? ?import java.io.FileNotFoundException;
?? ?import java.io.FileOutputStream;
?? ?import java.io.FileReader;
?? ?import java.io.FileWriter;
?? ?import java.io.IOException;
?? ?import java.io.InputStreamReader;
?? ?import java.io.OutputStreamWriter;
?? ?public class TransStreamDemo4 {
?? ??? ?public static void main(String[] args) throws Exception {
?? ??? ??? ??? ?readText2();
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?private static void readText2() throws Exception, IOException {
?? ??? ??? ?InputStreamReader fr=new InputStreamReader(
?? ??? ??? ??? ??? ?new FileInputStream("D:\\u8_1.txt"), "UTF-8");
?? ??? ??? ?char []ch=new char[10];
?? ??? ??? ?int len=fr.read(ch);
?? ??? ??? ??? ?String str=new String(ch,0,len);
?? ??? ??? ??? ?System.out.println(str);
?? ??? ??? ??? ?fr.close();
?? ??? ??? ?
?? ??? ?}
?? ??? ?private static void readText3() throws Exception {
?? ??? ??? ?FileReader fr=new FileReader("D:\\u8_1.txt");
?? ??? ??? ?char []ch=new char[10];
?? ??? ??? ?int len=fr.read(ch);
?? ??? ??? ??? ?String str=new String(ch,0,len);
?? ??? ??? ??? ?System.out.println(str);
?? ??? ??? ??? ?fr.close();
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?private static void writeText3() throws IOException, FileNotFoundException {
?? ??? ?OutputStreamWriter osw=new OutputStreamWriter(
?? ??? ??? ??? ?new FileOutputStream("D:\\u8_1.txt"), "UTF-8");
?? ??? ??? ??? ??? ??? ??? ??? ??? ?osw.write("小明");
?? ??? ??? ??? ??? ??? ??? ??? ??? ?osw.flush();
?? ??? ??? ??? ??? ??? ??? ??? ??? ?osw.close();
?? ??? ?}
?? ??? ?private static void writeText2() throws? Exception {
?? ??? ??? ?OutputStreamWriter fw=new OutputStreamWriter(
?? ??? ??? ??? ??? ?new FileOutputStream("D:\\gbk_1.txt"), "GBK");
?? ??? ??? ?
?? ??? ??? ??? ??? ??? ??? ??? ?fw.write("小明");
?? ??? ??? ??? ??? ??? ??? ??? ?fw.flush();
?? ??? ??? ??? ??? ??? ??? ??? ?fw.close();
?? ??? ?}
?? ??? ?private static void writeText() throws IOException {
?? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ?FileWriter fw=new FileWriter("D:\\gbk_1.txt");
?? ??? ??? ?//FileWriter其實(shí)就是轉(zhuǎn)換流制訂了本機(jī)默認(rèn)編碼表的體現(xiàn),可以更好的操作文本;
?? ??? ??? ?
?? ??? ??? ?/*
?? ??? ??? ? * 如果操作文本需要明確具體的編碼,必須要轉(zhuǎn)換流
?? ??? ??? ? *
?? ??? ??? ? * */
?? ??? ??? ?
?? ??? ??? ?fw.write("小明");
?? ??? ??? ?fw.flush();
?? ??? ??? ?fw.close();
?? ??? ?}
}?? ??? ?
?? ??? ??? ?
?? ?流的操作規(guī)律;
?? ??? ?自所以要弄清楚這個(gè)規(guī)律,是因?yàn)榱鞯膶?duì)象太多,開發(fā)時(shí)不知道用那個(gè)對(duì)象合適;
?? ??? ?想知道開發(fā)時(shí)用到哪些對(duì)象,只要通過四個(gè)明確即可;
?? ??? ?1,明確源和目的地;(匯)
?? ??? ??? ?源:InputStream? Reader;
?? ??? ??? ?目的: OutputStream Writer
?? ??? ?2,明確數(shù)據(jù)是否是純文本數(shù)據(jù);
?? ??? ??? ?源:是:純文本:Reader
?? ??? ??? ??? 否: InputStream
?? ??? ??? ?目的: 是純文本:Writer
?? ??? ??? ??? 否: OutputStream
?? ??? ?到這里就可以明確需求中具體要使用那個(gè)體系;
?? ??? ?3,明確具體設(shè)備;
?? ??? ??? ?源設(shè)備:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ??? ?鍵盤:System.in
?? ??? ??? ??? ?內(nèi)存:數(shù)組
?? ??? ??? ??? ?網(wǎng)絡(luò):Socket流
?? ??? ??? ?目的設(shè)備:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ??? ?鍵盤:System.in
?? ??? ??? ??? ?內(nèi)存:數(shù)組
?? ??? ??? ??? ?網(wǎng)絡(luò):Socket流
?? ??? ?4,是否需要其他額外功能.
?? ??? ??? ?1,是否需要高效(緩沖區(qū));
?? ??? ??? ??? ?是:buffer
?? ??? ?需求:
?? ??? ?1,復(fù)制一個(gè)文件:
?? ??? ??? ?1,明確源和目的:
?? ??? ??? ??? ?源:InputStream? Reader;
?? ??? ??? ??? ?目的:outputStream? Writer;
?? ??? ??? ?2,是否為純文本:
?? ??? ??? ??? ?是:Reader;
?? ??? ??? ??? ?目的:Writer;
?? ??? ??? ?3,明確設(shè)備:
?? ??? ??? ??? ?源:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ??? ?目的:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ?FileReader fr=new FileReader("a.txt");
?? ??? ??? ?FileWriter fw=new FileWriter("b.txt");
?? ??? ??? ?4,是否需要額外功能,
?? ??? ??? ?需要:
?? ??? ??? ?BufferedReader br=new BufferedReader(new FileReader("a.txt"));
?? ??? ??? ?BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
?? ??? ?需求:讀取鍵盤錄入信息,并寫入到一個(gè)文件中;
?? ??? ?1,明確源和目的.
?? ??? ??? ?源:InputStream Reader;
?? ??? ??? ?目的:OutputStream Writer;
?? ??? ?2,是否是純文本:
?? ??? ??? ?是;
?? ??? ??? ?源:Reader
?? ??? ??? ?目的:Writer;
?? ??? ?3,明確設(shè)備;
?? ??? ??? ?源:
?? ??? ??? ??? ?鍵盤:System.in
?? ??? ??? ?目的:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ?InputStream in=System.in;
?? ??? ??? ?FileWriter fw=new FileWriter("b.txt");
?? ??? ??? ?這樣做可以實(shí)現(xiàn),但是麻煩,將讀取的字節(jié)轉(zhuǎn)換成字符串,再有字符流操作;
?? ??? ?4,需要額外功能嗎?
?? ??? ??? ?需要;轉(zhuǎn)換;
?? ??? ??? ?InputStreamReader isr=new InputStreamReader(new InputStreamReader(System.in));
?? ??? ??? ?
?? ??? ?需求3;將一個(gè)文本文件數(shù)據(jù)顯示在控制臺(tái)上;
?? ??? ?1,明確源和目的.
?? ??? ??? ?源:InputStream Reader;
?? ??? ??? ?目的:OutputStream Writer;
?? ??? ?2,是否是純文本:
?? ??? ??? ?是;
?? ??? ??? ?源:Reader
?? ??? ??? ?目的:Writer;
?? ??? ?3,明確設(shè)備;
?? ??? ??? ?源:
?? ??? ??? ??? ?硬盤:File
?? ??? ??? ?目的:
?? ??? ??? ??? ?控制臺(tái):System.out
?? ??? ?FileReader fr=new FileReader("a.txt");
?? ??? ?OutputStream out=System.out;
?? ??? ?4,需要額外功能嗎?
?? ??? ??? ?需要;轉(zhuǎn)換;
?? ??? ??? ?FileReader fr=new FileReader("b.txt");
?? ??? ??? ?OutputStreamWriter osw=new OutputStreamWriter(System.out);
?? ??? ??? ?需要高效:
?? ??? ??? ?BufferedReader br=new BufferedReader(new FileReader("b.txt"));
?? ??? ??? ?BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
?? ??? ?==================================================================
?? ??? ?4,讀取鍵盤錄入數(shù)據(jù)顯示在控制臺(tái)上;
?? ??? ?1,明確源和目的.
?? ??? ??? ?源:InputStream Reader;
?? ??? ??? ?目的:OutputStream Writer;
?? ??? ?2,是否是純文本:
?? ??? ??? ?是;
?? ??? ??? ?源:Reader
?? ??? ??? ?目的:Writer;
?? ??? ?3,明確設(shè)備;
?? ??? ??? ?源:
?? ??? ??? ??? ?鍵盤:System.in
?? ??? ??? ?目的:
?? ??? ??? ??? ?控制臺(tái):System.out
?? ??? ??? ?inputStream in=System.in;
?? ??? ??? ?OutputStream out=System.out;
?? ??? ?4,額外功能?
?? ??? ?需要;
?? ??? ??? ?需要轉(zhuǎn)化,因?yàn)槎际亲止?jié)流,但是操作的確實(shí)文本數(shù)據(jù).
?? ??? ??? ?所以使用字符流操作起來更為便捷;
?? ??? ??? ?InputStreamReader isr=new InputStreamReader(System.in);
?? ??? ??? ?OutputStreamWriter osw=new OutputStreamWriter(System.out);
?? ??? ??? ?為了高效,
?? ??? ??? ?BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
?? ??? ??? ?BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(Sstem.out);
?? ???? //本人剛?cè)腴T菜鳥一枚,這些都是自己整理的資料,感覺I/O流體系太過于龐大,學(xué)了一些最常用的流操作方法,其他的流操作日后有時(shí)間在整理
//以供大家參閱
轉(zhuǎn)載于:https://www.cnblogs.com/pierceming/p/8711857.html
總結(jié)
- 上一篇: canvas生成二维码(2)
- 下一篇: SPSS新手教程——进行距离分析的方法