-2

what is the difference between the codes below?

//case01

Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext())
    {
        System.out.println("输出:"+sc.next());
    }

//case02

Scanner sc=new Scanner(System.in);
while(sc.hasNext())
{
    System.out.println("输出:"+sc.next());
}
ceo1207
  • 9
  • 4
  • 1
    Possible duplicate of [Scanner vs. BufferedReader](http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader) – Naman Mar 05 '17 at 07:00

2 Answers2

0

BufferedInputStream uses a buffer. You can say that it's mainly used for optimizing.

Kavya K
  • 11
  • 1
  • 4
0

A BufferedInputStream provides the most value when it is used to wrapper a stream that would incur overhead for each byte fetched, since it prefetches bytes a block at a time. For example when fetching from a hard drive, or even fetching from a socket (since it limits the overhead of calling the operating system for each and every byte read).

However, System.in is typically hooked to the keyboard. So that's a situation where there is little to no benefit to be gained by buffering. Another example would be if a stream was byte array based (such as a ByteArrayInputStream). No real benefit in either case.

Oscar
  • 334
  • 1
  • 10