据我所知,在Java中从文件中读取基于字符的数据的两种最常见的方法是使用Scanner或BufferedReader。我还知道BufferedReader通过使用缓冲区来避免物理磁盘操作,从而有效地读取文件。
我的问题是:
扫描器的性能和BufferedReader一样好吗? 为什么你会选择扫描器而不是BufferedReader,反之亦然?
据我所知,在Java中从文件中读取基于字符的数据的两种最常见的方法是使用Scanner或BufferedReader。我还知道BufferedReader通过使用缓冲区来避免物理磁盘操作,从而有效地读取文件。
我的问题是:
扫描器的性能和BufferedReader一样好吗? 为什么你会选择扫描器而不是BufferedReader,反之亦然?
当前回答
下面的答案来自从控制台读取:JAVA Scanner vs BufferedReader
当从控制台读取输入时,有两个选项可以实现这一点。首先使用Scanner,另一个使用BufferedReader。两者都有不同的特点。这意味着如何使用它的差异。
扫描器将给定的输入作为标记处理。BufferedReader只是将输入作为字符串逐行读取。Scanner本身提供了解析功能,就像nextInt()、nextFloat()一样。
但是,别人又有什么区别呢?
扫描器将给定的输入作为标记处理。BufferedReader作为流行/字符串。 使用正则表达式对给定输入进行扫描器标记化。使用BufferedReader必须编写额外的代码。 BufferedReader比Scanner更快。2 扫描仪未同步,BufferedReader已同步
从JDK 1.5及更高版本开始就有了Scanner。
什么时候应该使用扫描器,还是缓冲阅读器?
看看它们之间的主要区别,一个使用标记化,另一个使用流线。当您需要解析功能时,请改用Scanner。但是,我更喜欢BufferedReader。当您需要从文件中读取数据时,请使用BufferedReader,因为它在读取文件时使用缓冲内存,从而减少物理驱动器的使用。或者你可以使用BufferedReader作为Scanner的输入。
其他回答
请看这个链接,下面是引用自那里的:
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.
Scanner用于解析来自流内容的令牌,而BufferedReader只读取流,不做任何特殊的解析。
事实上,您可以将BufferedReader传递给扫描器作为要解析的字符源。
在当前最新的JDK 18发布/构建(b37)中,与BufferedReader(8192个字符)相比,Scanner的缓冲区更小(1024个字符),但这已经足够了。
至于选择,如果你想解析文件,使用Scanner,如果你想逐行读取文件,使用BufferedReader。也请参阅前面链接的API文档的介绍文本。
解析=将给定的输入解释为标记(部分)。它可以直接返回特定的部分,如int,字符串,小数等。请参见Scanner类中的所有nextXxx()方法。 阅读=无声流媒体。它不断返回给你所有的字符,你反过来必须手动检查,如果你想匹配或组合一些有用的东西。但如果你不需要这样做,那么阅读就足够了。
BufferedReader和Scanner之间的区别是:
BufferedReader reads data, but Scanner parses data. You can only read String using BufferedReader, using Scanner you can read to different data types like int. BufferedReader is older than Scanner, it was added on JDK 1.1, while Scanner was added on JDK 5 release. The buffer size of BufferedReader is larger (8KB) as compared to Scanner's 1KB. BufferedReader is more suitable for reading files with long String, while Scanner is more suitable for reading small user input from command prompt. BufferedReader is synchronized, while Scanner is not, which means you cannot share Scanner among multiple threads. BufferedReader is faster than Scanner because it doesn't spend time on parsing. BufferedReader is a bit faster as compared to Scanner. BufferedReader is from java.io package, while Scanner is from java.util package.
在此基础上,我们可以选择我们的选择。
感谢阅读!
BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream. Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String. BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads. Scanner hides IOException while BufferedReader throws it immediately.