/*
* 轉(zhuǎn)換流:
* 輸入字節(jié)流的轉(zhuǎn)換流:
* InputStreamReader() 輸入字節(jié)流到字符流 是字節(jié)流到字符流的通道,F(xiàn)ileReader還InputStreamReader的子類
*
* 輸出字節(jié)流的轉(zhuǎn)換流
* OutputStreamWriter()? 如果拿到的一個接口放回的是字節(jié)流蜡歹,那么就需要轉(zhuǎn)換成字符流輸出
*
* 作用:
* 1.如果目前獲得是是字節(jié)流灶挟,需要轉(zhuǎn)換成字符流才能使用圾旨,那么就使用轉(zhuǎn)換字符流摘悴;
*? 2.使用轉(zhuǎn)換流可以指定編碼集,進(jìn)行讀寫文件碾篡;
*/
package com.michael.lin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Demo707 {
public static void main(String[] args) throws IOException{
readLine();
writeLine();
}
//輸入字節(jié)轉(zhuǎn)換流
public static void readLine() throws IOException{
InputStream in= System.in; // in是一個InputStream類的匇InputStream
//System.out.println("讀到的數(shù)據(jù)是:" + (char)in.read());
//把字節(jié)流轉(zhuǎn)換成字符流 -- 轉(zhuǎn)換流
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
System.out.println("您輸入的是:" + bufferedReader.readLine());
}
//輸出字節(jié)轉(zhuǎn)換流
public static void writeLine() throws IOException{
File file = new File("c:\\b.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//把輸出字節(jié)流轉(zhuǎn)換為輸出字符流
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.write("你好虱而,吃飯了嗎");
outputStreamWriter.close();
}
}