黑馬程式設計師 —— Java高階視訊_IO輸入與輸出(第十九天)2
-------
android培訓、java培訓、期待與您交流! ----------
十八 流操作規律1
上面學習瞭如果之多的流(其實後面的章節還有不少),那麼到底應該如何確定什麼情況用什麼流呢?
關鍵是要掌握了流操作的基本規律,掌握了規律操作起來就容易了。
1、引入“源和目的”的概念
import java.io.*; public class Demo { public static void main(String args[]) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader( System.in)); //這裡是“源” BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter( System.out)); //這裡是“目的” String line = null; while ((line = bufr.readLine()) != null) { if (line.equals("over")) { break; } bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } bufr.close(); } }
上面的程式,我們剛開始源和目的是 —— 源:鍵盤錄入、目的:控制檯,
如果我們需求改變了,需求:想把鍵盤錄入的資料儲存到一個檔案中 —— 源:鍵盤錄入、目的:檔案,
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo.txt")));
如果需求在此發生變化,需求:將一個檔案的資料列印到控制檯上 —— 源:檔案、目的:控制檯,
BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("CopyPic.java"))); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
可見,“源和目的”不一樣,所使用的流物件也不一樣。
2、流的操作規律
(1) 明確源和目的- 源:輸入流 — InputStream、Reader
- 目的:輸出流 —OutputStream、Writer
- 是:字元流
- 不是:位元組流
- 源裝置:記憶體、硬碟、鍵盤
- 目的裝置:記憶體、硬碟、控制檯
- 是否需要提高效率:是則加入緩衝區技術(推薦加入);否則不加
注意:(1)、(2)是用來明確體系,(3)是用來決定使用物件的。
3、通過具體需求掌握規律
需求:複製檔案,將一個文字檔案中的資料儲存到另一個檔案中。(1) 源
因為是源,所以使用讀取流,IputStream或者Reader
是不是操作文字檔案:是,所以使用 Reader
這樣就明確了體系,接下來明確要使用該體系中的那個物件。
明確裝置,硬碟上的一個檔案 → 硬碟
Reader 體系中可以操作檔案的是,FileReader
FileReader fr = new FileReader("a.txt");
是否需要提高效率?
BufferedReader bufr = new BufferedReader(fr);
(2) 目的
寫入流:OutputStream或者Writer
是否純文字:是Writer
裝置:硬碟上的一個檔案 → 硬碟
Writer體系中可以操作檔案的物件 FileWriter
FileWriter fw = new FileWriter("b.txt");
是否需要提高效率?是: BufferedWriter bufr = new BufferedWriter(fr);
練習:複製檔案,將一個圖片檔案中的資料儲存到另一個檔案中。要按照以上格式自己完成三個明確。
十九 流操作規律2
1、分析需求
需求:將鍵盤錄入的資料儲存到一個檔案中。
這個需求中有源和目的都存在,那麼分別分析
(1) 源
InputStream或者Reader
是不是純文字:是,用Reader
是不是裝置:是,鍵盤,對應的物件是 System.in
不是選擇Reader嗎?System.in 對應的不是位元組流嗎?
為了操作鍵盤的文字資料方便,位元組流可以轉換成字元流,按照字串操作最方便。
既然明確了 Reader 就將 System.in 轉換成 Reader
用到了Reader 體系中的轉換流,InputSteamReader
InputStreamReader isr = InputStreamReader(System.in);
需要提高效率嗎?需要,BufferedReader
BufferedReader bufr = new BufferedReader(isr);
(2)目的:
OutputStream或者Writer
是否是純文字?是,Writer
是不是裝置?是,硬碟,使用FileWriter
FileWriter fw = new FileWriter("b.txt");
需要提高效率嗎?需要
BufferedWriter bufw = new BufferedWriter(fw);
2、擴充套件一下
想要把錄入的資料按照指定的編碼表(UTF-8),將資料存到檔案中。(使用轉換流,可以指定編碼)
目的:OutputStream Writer
是否是純文字?是,Writer
是不是裝置?是,硬碟,使用FileWriter
但是儲存時需要加入指定的編碼表,FileWriter預設使用GBK編碼表。對於指定編碼表,只有轉換流可以指定。
所以要使用的物件是 OutputStreamWriter.
而該轉換流物件要接收一個位元組輸出流。而且還可以操作檔案的位元組輸出流,FileOutputStream
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d.txt"),"UTF-8");
需要高效嗎?需要,BufferedWriter bufw = new BufferedWriter(osw);
所以要記住,轉換流什麼時候使用,字元和位元組之間的橋樑,通常,涉及到字元編碼轉換時,需要用到轉換流。
練習:將一個文字資料列印在控制檯上。要按照以上格式自己完成三個明確。
二十 改變標準輸入輸出裝置
通過System類中的setIn()、setOut()方法可以改變裝置,
例如,將鍵盤錄入改成檔案,System.setIn(new FileInputStream("buf.txt"));
將輸出目的改為檔案,System.setOut(new PrintStream("buf.txt"));
如果源和目的都改成檔案,就是複製。
- static void setIn(InputStream in)重新分配“標準”輸入流。
- static void setOut(PrintStream out)重新分配“標準”輸出流。
import java.io.*;
public class Demo {
public static void main(String args[]) throws IOException {
System.setIn(new FileInputStream("ArrayToolDemo.java"));
System.setOut(new PrintStream("buf2.txt"));
}
}
二十一 異常的日誌資訊
import java.io.*;
import java.util.*;
import java.text.*;
public class Demo {
public static void main(String args[]) {
try {
int[] arr = new int[2];
System.out.println(arr[3]);
} catch (Exception e) {
try {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
PrintStream ps = new PrintStream("exeception.txt");
ps.println(s);
System.setOut(ps);
} catch (IOException ex) {
throw new RuntimeException("日誌檔案建立失敗");
}
e.printStackTrace(System.out);
}
}
}
異常中,異常物件呼叫printStackTrace(),將異常資訊列印在控制檯,底層呼叫System.out,
或者建立一個PrintStream物件,將資訊列印在控制檯。PrintStream也可以呼叫println()方法。
需要將異常資訊列印在某個檔案中,便於觀察,
例如,System.setOut(newPrintStream("exception.log"));e.printStackTrace(System.out);最好在加上時間。
一般使用網路上的log4j工具,幫助完成日誌資訊的建立。
二十二 系統資訊
import java.io.*;
import java.util.*;
import java.text.*;
import java.text.*;
public class Demo {
public static void main(String args[]) throws IOException {
Properties prop = System.getProperties();
prop.list(new PrintStream("demo.txt"));
}
}
static Properties getProperties():確定當前的系統屬性。
------- android培訓、java培訓、期待與您交流! ----------