讀取鍵盤輸入
示例1:
下圖中第3行建立字節讀取流對象,獲取鍵盤讀取流System.in。第4行使用字節讀取流的read()方法來讀鍵盤數據。第5行進行輸出。
private static void readKey1() throws IOException
{
InputStream in = System.in;
int ch = in.read();
System.out.println(ch);
}
示例2:下圖中使用循環讀取鍵盤輸入的數據。當鍵入數據時按下回車鍵,此時回車鍵也會被讀取進去,並打印出對於的ACSII碼。如果輸入的是漢字,則會分成兩個字節讀取。
public static void readKey2() throws IOException
{
InputStream in = System.in;
int ch = 0;
while((ch = in.read()) != -1)
{
System.out.println(ch);
}
}
獲取用戶鍵盤錄入的數據,並將數據變成大寫顯示在控制檯上,如果輸入over,結束鍵盤錄入。
public static void readKey3() throws IOException
{
//1.創建容器
StringBuilder sb = new StringBuilder();
//2.獲取鍵盤讀取流
InputStream in = System.in;
//3.定義變量,記錄讀取到的字節,並循環獲取。
int ch = 0;
while((ch = in.read()) != -1)
{
//在存儲之前需要判斷是否是換行標記,因爲換行標記不存儲。
if(ch == '\r')
{
continue;
}
if(ch == '\n')
{
String temp = sb.toString();
if("over".equals(temp))
{
break;
}
System.out.println(temp.toUpperCase());
sb.delete(0, sb.length());
}
else
{
//將讀取到的字節存儲到StringBuilder中
sb.append((char)ch);
}
}
}
轉換流:
InputStreamReader:字節到字符的橋樑。解碼。
OutputStreamWriter:字符到字節的橋樑。編碼。
下圖中第4行定義了一個轉換流對象,構造函數中傳遞的是一個字節流,就把字節流轉換成字符流了。然後在用字符緩衝區進行從鍵盤讀取數據的操作。第8行定義了一個轉換流對象,把字節流轉換成爲字符流。其實就是上個例子的另一個實現方式。
public static void demo1() throws IOException
{
InputStream in = System.in;
InputStreamReader isr = new InputStreamReader(in);
BufferedReader bufr = new BufferedReader(isr);
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bufw = new BufferedWriter(osw);
String line = null;
while((line = bufr.readLine()) != null)
{
if("over".equals(line))
{
break;
}
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
}
轉換流的編碼和解碼:
下圖中writeText_1()方法使用字符流操作文件,將“你好”寫到gbk.txt中。使用的碼錶是默認碼錶GBK,寫入後文件是4個字節,即每個漢字是2個字節。writeText_2()方法使用轉換流,使用碼錶是指定碼錶UTF-8,將“你好”寫入文件後是6個字節,即每個漢字是3個字節。用轉換流對文件進行讀寫纔可以指定編碼表。
public static void writeText_1() throws IOException
{
FileWriter fw = new FileWriter("gbk.txt");
fw.write("你好");
fw.close();
}
public static void writeText_2() throws IOException
{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"), "UTF-8");
osw.write("你好");
osw.close();
}
下圖中readText_1()方法使用默認的編碼表GBK讀取文件gbk.txt,因爲上圖中是用相同編碼表把字符串寫到文件中去。readText_2()方法使用了指定的編碼表"UTF-8"來讀取文件u8_1.txt,因爲上圖中使用相同的編碼表把字符串寫到文件中去。如果使用不同的編碼表進行編碼解碼,那可能讀出的東西不是你想要的結果。
public static void readText_1() throws IOException
{
FileReader fr = new FileReader("gbk.txt");
char[] buf = new char[10];
int len = fr.read(buf);
String str = new String(buf, 0, len);
System.out.println(str);
fr.close();
}
public static void readText_2() throws IOException
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("u8_1.txt"), "UTF-8");
char[] buf = new char[10];
int len = isr.read(buf);
String str = new String(buf, 0, len);
System.out.println(str);
isr.close();
}
流的操作規律:
之所以要弄清楚這個規律,是因爲流對象太多,開發時不知道用哪個對象合適。
想要知道開發時用到哪些對象,只要通過四個明確即可。
1、明確源和目的(匯)
源:InputStream Reader
目的:OutputStream Writer
2、明確數據是否是純文本數據。
源:
是純文本: Reader
不是純文本: InputStream
目的:
是純文本: Writer
不是純文本: OutputStream
3、明確具體的設備。
源設備:
硬盤:File
鍵盤:System.in
內存:數組
網絡: Socket流
目的設備:
硬盤:File
控制檯:System.out
內存:數組
網絡:Socket流
4、是否需要其他額外功能
(1)是否需要高效(緩衝區)
是,就加上buffer
(2)轉換