java中講講InputStreamReader的用法
InputStreamReader的用法
馬克-to-win:InputStreamReader 從文字上分析:InputStream是字節(jié)流的意思,Reader是字符流的意思。InputStreamReader這個類就是用來把字節(jié)流轉換成字符流的。System.in代表控制臺輸入。它天生是個字節(jié)流。參見我前面寫的InputStream小節(jié)的例:2.1.1,我們發(fā)現(xiàn)如果向控制臺輸入中文,控制臺是處理不了的,但這時如果我們用InputStreamReader這個工具轉換一下,問題就解決了。下一章我們要講的網(wǎng)絡傳輸,天生也是以字節(jié)形式進行的,所以字節(jié)流和字符流之間也必然轉換一下。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
例:2.4.1
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
// ////////////////from console to string,\u00A0
System.out.println("from console to string ");
/*An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted. InputStreamReader(InputStream in) Create an
InputStreamReader that uses the default charset.
*/
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String stdinS;
stdinS = stdin.readLine();
System.out.println(stdinS);
}
}
結果是:
from console to string
我們正在學java
我們正在學java