IO流(读取键盘录入)

 1 package com.yyq;
 2 import java.io.*;
 3 /*
 4  * 字节流 FileInputStream
 5  *     FileOutputStream
 6  *     BufferedInputStream
 7  *     BufferedOutPutStream
 8  */
 9 /*
10  * 读取键盘录入: 
11  *   system.out: 对应的是标准的输出设备,控制台
12  *   system.in: 对应的是标准的输入设备,键盘 
13  */
14 /*
15  * 需求: 通过键盘录入数据,录入一行数据打印,
16  * 录入over 停止。
17  */
18 public class TransStreamDemo {
19 
20     public static void main(String[] args) throws IOException {
21         // TODO Auto-generated method stub
22         //System.in 是字节流 对应的类型是inputStream
23         InputStream in = System.in;
24         // 此处的read 方法是一个阻塞式方法,等待我的键盘录入
25         // 被提升 这是键盘录入
26         StringBuilder sb = new StringBuilder();
27         while(true){
28             int ch = in.read();
29             if(ch =='\r'){
30                 continue;
31             }
32             if(ch=='\n'){
33                 String s = sb.toString();
34                 if("over".equals(s)){
35                     break;
36                 }
37                 System.out.println(s.toUpperCase());
38                 sb.delete(0,sb.length());
39             }
40             else{
41                 sb.append((char)ch);
42             }
43         }
44     }
45 
46 }

 

posted @ 2016-01-23 14:44  HuberyQian  阅读(665)  评论(0编辑  收藏  举报