java中IO(六):BufferedReader、Scanner简单讲解

java中IO(六):BufferedReader、Scanner简单讲解

介绍:

在io(五)中讲到使用光使用System.io进行输入会比较麻烦,所以我们本次介绍BufferedReaderScanner来对上面的问题进行解决。

讲解

一、BufferedReader类

类的定义:

public class BufferedReader
extends Reader

构造方法:

BufferedReader(Reader in)
//Creates a buffering character-input stream that uses a default-sized input buffer.
BufferedReader(Reader in, int sz)
//Creates a buffering character-input stream that uses an input buffer of the specified size.

其他方法:

String	readLine()
//Reads a line of text.

​ 通过类的继承关系和构造函数,大致弄懂了BufferedReader使用的应该是装饰设计模式,增强了Reader的功能,在这里为什么要写一下readLine这个函数呢,因为这个函数比较好用我们平时使用也比较多。

​ 想使用BufferedReader解决一下System.in的输入问题,那问题来了System.inInputStream类型,但是BufferedReader接收的是Reader类型,按给怎么处理呢?

​ 其实也很简单,前面我们介绍转换流InputStreamReader起到了作用。

代码:

public static void main(String[] args) {
      BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("请输入:");
      try {
          String str = bReader.readLine();//默认以回车换行
          System.out.println("刚刚输入的是:"+str);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
}

结果:

请输入:
hello world
刚刚输入的是:hello world

​ 这样就能够实现系统的输入了,并且还比较好使用,除此之外BufferedReader可以处理File,也比较方便。

​ 但是其实BufferedReader也有一个问题:默认必须是回车换行才算结束

​ 因为仅仅是回车结束,其实有时候问题也会有些复杂,比如想提取文本中的单词,仅仅使用BufferedReader读取每一行之后还需要对每一行进行处理,使用起来也比较麻烦。所以这就显示出了Scanner的作用了,Scanner对象把回车,空格,tab键都看作输入结束,这样就极大的方便了我们提取信息。

二、Scanner类

Scanner是java.util包中的东西,并不在java.io包当中,但是用它处理输入流,尤其是系统输入时比较方便的

构造方法:

Scanner(InputStream source)
//Constructs a new Scanner that produces values scanned from the specified input stream.
    
Scanner(InputStream source, String charsetName)
//Constructs a new Scanner that produces values scanned from the specified input stream.

可以看到构造方法可以接收输入流。

其他方法:

boolean	hasNext()
//Returns true if this scanner has another token in its input.
    
boolean	hasNextFloat()
//Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.
    
boolean	hasNextInt()
//Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
    
String	next()
//Finds and returns the next complete token from this scanner.
    
float	nextFloat()   
//Scans the next token of the input as a float.
    
int	nextInt()
//Scans the next token of the input as an int.

方法只是列出部分,但是光是看这些方法都感觉很顶,所以这也是为什么Scanner取代BufferedReader进行输入流的读取。

代码:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        if (scanner.hasNext() ){
            System.out.println("刚刚输入的信息为:"+scanner.nextLine());
        }
       
    }

结果:

请输入:
hello world
刚刚输入的信息为:hello world

可以看到也能实现很简单的系统输入,但是除此之外,Scanner还提供了判断是否有int型等功能,使用起来肯定是比BufferedReader顺手的。并且Scanner也可以处理File输入,所以在很多方面的功能上BufferedReader都被Scanner取代了。

总结

总结了一下BufferedReader和Scanner,两者在输入上都有作用,但是一般来说现在使用Scanner进行读取更多一些,因为比较方便,但是BufferedReader好像在IO读取上效率更高,这个暂时没有研究,如果后面有时间,会再进行研究一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值