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  * 其实就是readLine方法,直接使用readLine方法
18  * 来完成键盘录入的一行数据的读取
19  * readLine 是 BufferedReader 类的方法(字符流)
20  * 而键盘录入的方法是InputStream的方法 (字节流)
21  * 能不能将字节流转成字符流再使用字符流缓冲区中的方法呢??
22  * 转换流 : 两个流  
23  *         在字符流体系中
24  *         InputStreamReader 将字节流转换成字符流(字节流通向字符流的桥梁)
25  *         通过它能将字节流变成字符流
26  * (将字节流转换成字符流)
27  *  1. 获取键盘录入对象
28  *  2.将字节流对象转换成字符流对象,(字符流的缓冲区 是用来装饰的)
29  *  InputStreamReader isr = new InputStreamReader(in);
30  *  3.为了提高效率,将字符串进行缓冲区技术高效操作,使用bufferedreader
31  *  BufferedReader bufr = new BufferedReader(isr);
32  */
33 public class TransStreamDemo {
34 
35     public static void main(String[] args) throws IOException {
36         // TODO Auto-generated method stub
37         //System.in 是字节流 对应的类型是inputStream
38         InputStream in = System.in;
39         BufferedReader fr = new BufferedReader(new InputStreamReader(System.in));
40         while(true){
41             String line = fr.readLine();
42             if(line.equals("over")){
43                 break;
44             }
45             System.out.println(line);    
46         }
47         // 此处的read 方法是一个阻塞式方法,等待我的键盘录入
48         // 被提升 这是键盘录入
49     /*    StringBuilder sb = new StringBuilder();
50         while(true){
51             int ch = in.read();
52             if(ch =='
'){
53                 continue;
54             }
55             if(ch=='
'){
56                 String s = sb.toString();
57                 if("over".equals(s)){
58                     break;
59                 }
60                 System.out.println(s.toUpperCase());
61                 sb.delete(0,sb.length());
62             }
63             else{
64                 sb.append((char)ch);
65             }
66         }*/
67     }
68 
69     private static String InputStreamReader(InputStream in) {
70         // TODO Auto-generated method stub
71         return null;
72     }
73 
74 }
原文地址:https://www.cnblogs.com/yangyongqian/p/5153331.html