码迷,mamicode.com
首页 > 编程语言 > 详细

毕向东_Java基础视频教程第19天_IO流(15~16)

时间:2017-01-27 15:05:32      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:字符   int   not   控制台   else   数据   使用   视频教程   write   

第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);
        }
    }

}

 

毕向东_Java基础视频教程第19天_IO流(15~16)

标签:字符   int   not   控制台   else   数据   使用   视频教程   write   

原文地址:http://www.cnblogs.com/echo1937/p/6353845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!