据我所知,在Java中从文件中读取基于字符的数据的两种最常见的方法是使用Scanner或BufferedReader。我还知道BufferedReader通过使用缓冲区来避免物理磁盘操作,从而有效地读取文件。
我的问题是:
扫描器的性能和BufferedReader一样好吗? 为什么你会选择扫描器而不是BufferedReader,反之亦然?
据我所知,在Java中从文件中读取基于字符的数据的两种最常见的方法是使用Scanner或BufferedReader。我还知道BufferedReader通过使用缓冲区来避免物理磁盘操作,从而有效地读取文件。
我的问题是:
扫描器的性能和BufferedReader一样好吗? 为什么你会选择扫描器而不是BufferedReader,反之亦然?
当前回答
请看这个链接,下面是引用自那里的:
A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. Each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Efficiency is improved appreciably if a Reader is warped in a BufferedReader. BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads. A scanner on the other hand has a lot more cheese built into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter! A scanner however is not thread safe, it has to be externally synchronized. The choice of using a BufferedReader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice. Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.
其他回答
BufferedReader可能会给你更好的性能(因为Scanner是基于InputStreamReader,查看源代码)。哎呀,对于从文件中读取数据,它使用nio。当我测试nio的性能与大文件的BufferedReader性能时,nio显示出更好的性能。 要从文件中读取数据,请尝试Apache Commons IO。
我更喜欢Scanner,因为它不会抛出检查异常,因此它的使用结果在一个更精简的代码。
BufferedReader和Scanner的区别如下:
BufferedReader已同步,但Scanner未同步。 BufferedReader是线程安全的,但Scanner不是。 BufferedReader有更大的缓冲内存,而Scanner有更小的缓冲内存。 BufferedReader执行速度更快,但Scanner执行速度较慢。 从控制台读取一行的代码:
BufferedReader:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String st = br.readLine();
// You can make the object InputStreamReader object inside the BufferReader method.
BufferReader br = new BufferedReader(InputStreamReader(System.in));
String st = br.readLine();
// You can even inspect the type of the input stream manually by using Parse method which accepts string parameter.
int x = Integer.parseInt(br.readLine());
// Or you can pass the object directly.
int x = Integer.parseInt(st);
扫描仪:
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
在java中有不同的输入方式,比如:
1) BufferedReader 2) Scanner 3)命令行参数
BufferedReader从字符输入流中读取文本,缓冲字符,以便有效地读取字符、数组和行。
Where Scanner是一个简单的文本扫描程序,可以使用正则表达式解析基本类型和字符串。
如果您正在编写一个简单的日志读取器,缓冲读取器就足够了。如果您正在编写XML解析器,那么Scanner是更自然的选择。
如需更多信息,请参考:
http://java.meritcampus.com/t/240/Bufferedreader?tc=mm69
请看这个链接,下面是引用自那里的:
A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. Each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Efficiency is improved appreciably if a Reader is warped in a BufferedReader. BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads. A scanner on the other hand has a lot more cheese built into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter! A scanner however is not thread safe, it has to be externally synchronized. The choice of using a BufferedReader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice. Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.