轉換流(InputStreamReader和OutputStreamWrite )
阿新 • • 發佈:2019-01-26
- InputStreamReader將位元組輸入流轉換成字元輸入流
- OutputStreamWriter將位元組輸出流裝換成字元輸出流
Tips:字元流比位元組流操作方便
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public static void main(String[]args)throws IOException{
//將System.in物件轉換成Reader物件
InputStreamReader ISR = new InputStreamReader(System.in);
//將普通的Reader包裝成BufferedReader,BufferedReader流具有緩衝功能
BufferedReader BR = new BufferedReader(ISR);
System.out.println("What's your name?");
String userInput = BR.readLine(); //program waits here until the user inserts a line of text
System.out.println("Your name is : "+userInput);
BR.close();
ISR.close();
}
}