标签:
import java.io.IOException; import java.io.InputStream; /** *读取键盘录入的数据,并打印在控制台上。 * *键盘本身就是一个标准的输入设备, *对于Java而言, */ public class ReadKey { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // readKey(); readKey_2(); } public static void readKey() throws IOException { InputStream in = System.in; int ch = in.read(); System.out.println(ch); int ch1 = in.read(); System.out.println(ch1); int ch2 = in.read(); System.out.println(ch2); } /** * 获取用户键盘录入的数据, * 并将数据变成大写显示在控制台上, * 如果用户输入的是over,则结束键盘录入 * * 思路: * 1,因为键盘录入只读取一个字节,要判断是否是over,需要先将读到的字节拼成字符串 * 2,那就需要一个容器。可以使用StringBuilder * 3,在用户回车之前将录入的数据变成字符串判断即可。 * @throws IOException */ public static void readKey_2() throws IOException { //创建容器 StringBuilder sb = new StringBuilder(); //获取键盘读取流 InputStream in = System.in; //定义变量记录读取到的字节,并循环读取。 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{ sb.append((char)ch); } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class TransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //字节流 InputStream in = System.in; //将字节转换成字符的桥梁,即转换流。 InputStreamReader isr = new InputStreamReader(in); //字符流 BufferedReader bufr = new BufferedReader(isr); String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)){ break; } System.out.println(line.toUpperCase()); } } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; public class TransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) 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(); } } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class TransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)){ break; } bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class TransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt"))); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt"))); String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)){ break; } bufw.write(line); bufw.newLine(); bufw.flush(); } } }
标签:
原文地址:http://www.cnblogs.com/lingfengblogs/p/4190770.html