- /*读取键盘录入
- 当录入一行数据,将该行数据打印
- */
- import java.io.*;
- class ReadIn
- {
- public static void main(String[] args) throws IOException
- {
- InputStream in = System.in;
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- int ch = in.read();
- if (ch=='\r')
- continue;
- if(ch=='\n')
- {
- String s = sb.toString();
- if("over".equals(s))
- break;
- System.out.println(s.toUpperCase());
- sb.delete(0,sb.length());
- }
- else
- sb.append((char)ch);
- }
- }
- }
- 注意:键盘录入要么Ctrl+c或者定义一个结束标记 来结束录入
- 优化以后的:使用转换流
- /*
- 使用转换流
- */
- import java.io.*;
- class TranStreamDemo
- {
- public static void main(String[] args) throws IOException
- {
- //获取键盘录入对象
- InputStream in = System.in;
- //将字节流对象转成字符流对象,使用转换流:InputStreamReader()
- InputStreamReader isr = new InputStreamReader(in);
- //为了提高效率,将字符流装进缓冲区。使用BufferedReader()
- BufferedReader bufr = new BufferedReader(isr);
- String len = null;
- while ((len = bufr.readLine())!=null)
- {
- if("over".equals(len))
- break;
- System.out.println(len.toUpperCase());
- }
- bufr.close();
- }
- }
复制代码
|
|