做網站和網頁區(qū)別/seo黑帽教學網
-
標準輸入輸出流
1. System.in 標準輸入流
? ? ? ? 本質上是一個InputString,對應鍵盤,表示從鍵盤輸入。
????????定義:public final static InputStream in = null;
? ? ? ? 所以?Scanner scanner = new Scanner(System.in); 會從鍵盤中獲取數據
2.?System.out 標準輸出流
? ? ? ? 本質上是一個OutputStream,對應顯示器,不過被包裝成了PrintStream,表示在顯示器上輸出。
????????定義:public final static PrintStream out = null;
? ? ? ? 所以?System.out.println(); 就是使用 out 對象將數據輸出到顯示器上。
-
轉換流
1. 為了解決使用讀寫源文件時出現(xiàn)的亂碼問題(字符編碼不一致),引出轉換流
2.?InputStreamReader 是 Reader 的子類,代碼可以將 InputStream 字節(jié)流包裝(轉換)成指定編碼的 Reader 字符流
3. OutputStreamWriter 是 Writer 的子類,可以將 OutputStream 字節(jié)流包裝(轉換)成指定編碼的 Writer 字符流
4. 當處理存文本數據時,使用字符流的效率更高
-
InputStreamReader 類的常用方法
1.?使用特定編碼方式包裝字節(jié)輸入流的構造方法
????????public InputStreamReader(InputStream in, Charset cs)
????????參數:in - 一個 InputStream 子類下的輸入流
? ? ? ? ? ? ? ? ? ?cs - 一個字符串類型的字符集
2. 獲得該轉換流所使用的編碼方式
????????public String getEncoding()
3. 關閉流
????????public void close() throws IOException
? ? ? ? 說明:不管包裝了多少個流,只需要關閉最外層的流就行
public class InputStreamReader01 {public static void main(String[] args) throws IOException {// 演示使用轉換流解決中文亂碼的問題, 通過轉換流 將字節(jié)流 轉換成指定編碼的字符流String filePath = "d:\\a.txt";FileInputStream fis = new FileInputStream(filePath);// 將字節(jié)流轉換成轉換流// InputStreamReader isr = new InputStreamReader(fis, "UTF-8");InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);// 將轉換流又轉換成包裝流BufferedReader br = new BufferedReader(isr);// 讀取String s = br.readLine();System.out.println("編碼方式為: " + isr.getEncoding());System.out.println("讀取到的第一行為: " + s);br.close();}
}
-
OutputStreamWriter 類的常用方法
1.?使用特定編碼方式包裝字節(jié)輸出流的構造方法
????????public OutputStreamWriter(OutputStream out, Charset cs)
? ? ? ? 參數:out - 一個 OutputStream 子類下的輸出流
? ? ? ? ? ? ? ? ? ?cs - 一個字符串類型的字符集
2. 關閉流
????????public void close() throws IOException
? ? ? ? 說明:不管包裝了多少個流,只需要關閉最外層的流就行,如果不關閉或刷新流,東西寫不進去
public class OutputStreamWriter01 {public static void main(String[] args) throws IOException {String filePath = "d:\\a.txt";FileOutputStream fos = new FileOutputStream(filePath, true);OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);BufferedWriter fw = new BufferedWriter(osw);fw.newLine();fw.write("通過轉換流寫入");fw.close();}
}