第19天-15-IO流(读取键盘录入)
第19天-16-IO流(读取转换流)
package bxd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /* 字符流 FileReader FileWriter BufferedReader BufferedWWriter 字节流 FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream */ public class TransStreamDemo { public static void main(String[] args) throws IOException { method(); anotherMethod(); } //使用readLine方法来实现: readLine()是字符流BufferedReader的方法, 而System.in的read()是字节流InputStream的方法 public static void method() throws IOException { InputStream in = System.in; InputStreamReader inr = new InputStreamReader(in); BufferedReader bufr = new BufferedReader(inr); String line = null; while ((line = bufr.readLine()) != null) { if ("over".equals(line)) break; System.out.println(line.toUpperCase()); } bufr.close(); } //通过键盘录入数据. 当录入一行数据后, 就将该行数据进行打印; 当录入的数据是over, 那么停止录入. //System.in: 对应的是标准输入设备, 键盘; System.out: 对应的是标准输出设备, 控制台 public static void anotherMethod() 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); } } }
时间: 2024-11-02 21:38:36