将 File 或 FileReader 与扫描仪一起使用?
免责声明:我已经浏览了我能找到的所有问题,但没有一个问题能回答这个确切的问题。如果您找到了,请指出并保持礼貌。
因此,Oracle I/O 教程 打开一个文本文件扫描程序如下:
new Scanner(BufferedReader(FileReader("xanadu.txt")));
但是 Javadoc 打开一个带有扫描仪的文本文件如下
new Scanner(new File("myNumbers"));
:很高兴使用更简单的方法,特别是当我有一个小文件并且可以使用较小的缓冲区时,但我也看到人们说,当您直接打开文件时,您会 无法关闭。如果是这样的话,为什么官方文档中使用这个习惯用法?
编辑:我也见过 new Scanner(FileReader("blah.txt")); 但这似乎是两全其美的。
编辑:我并不是想引发关于是否使用扫描仪的争论。我有一个关于如何使用扫描仪的问题。谢谢。
Disclaimer: I've looked through all the questions I can find and none of them answers this exact question. If you find one please point me to it and be polite.
So, the Oracle I/O tutorial opens a text file with Scanner as follows:
new Scanner(BufferedReader(FileReader("xanadu.txt")));
But the Javadoc opens a text file with Scanner like this:
new Scanner(new File("myNumbers"));
It would be nice to use the simpler method, especially when I have a small file and can live with the smaller buffer, but I've also seen people say that when you open a File directly you can't close it. If that's the case, why is that idiom used in the official documentation?
Edit: I've also seen new Scanner(FileReader("blah.txt"));
but this seems like the worst of both worlds.
Edit: I'm not trying to start a debate about whether to use Scanner or not. I have a question about how to use Scanner. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以查看 Scanner 的实现(JDK 附带源代码)。 Scanner 类中也有一个 close() 方法。本质上,您列出的两种方法对于读取小文件的用例来说是相同的 - 只是不要忘记在最后调用 close() 。
You could look at implementation of Scanner (JDK is shipped with source code). There is a close() method in Scanner class as well. Essentially both approaches you listed are identical for your use case of reading small file - just don't forget to call close() at the end.
File
类没有close()
方法,因为它仅抽象磁盘文件。它不是文件的输入流,因此无需关闭任何内容。The
File
class has noclose()
method because it only abstracts a disk file. It is not an input stream to the file, so there is nothing to close.是的,你可以这样做。
基本上你会这样做:
读取字符串:
完成文件后,执行以下操作
Yes you can do that.
Basically you do:
To read a String:
When you are done with the file, do
课程用马。在 Scanner javadocs 中,Scanner 是“一个简单文本扫描器,可以使用正则表达式解析基本类型和字符串”。因此,我对您的问题的看法是:无论您使用哪种方法,文件的更简单选项与 Oracle 教程中的选项一样好。扫描仪用于方便地标记文本文件,如果您的文件很小,正如您所说,那么它非常适合。
由于扫描程序使用正则表达式,因此无论您是否为扫描程序创建缓冲文件读取器,您都不能真正期望它具有巨大的性能。底层的 Readable 将是 close()d (如果它是 Closeable,如果您使用 Scanner(File) 构造函数,那么它就是 Closeable),因此只要您 close() Scanner 对象,您就不必担心(或使用尝试资源)。
Horses for courses. From the Scanner javadocs, a Scanner is "A simple text scanner which can parse primitive types and strings using regular expressions." So, my take on your question is: it does not matter which approach you use, the simpler option with File is just as good as the one found in Oracle tutorials. Scanner is for convenient tokenizing of text files, and if your file is small, as you said, than it's a perfect fit.
Because a Scanner uses regular expressions, you can't really expect huge performance with it, whether you create a buffered file reader for the scanner or not. The underlying Readable will be close()d (if it's a Closeable, which it will be, if you use the Scanner(File) constructor), and so you don't have to worry as long as you close() your Scanner object (or use try-with-resources).
有多种方法可以构造 Scanner 对象。
http://docs.oracle.com/javase /1.5.0/docs/api/java/util/Scanner.html
我个人甚至不会使用 Scanner 来读取文件。查看 BufferedReader 教程。这并不难弄清楚。
There are multiple ways to construct a Scanner object.
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
I personally wouldn't even use Scanner for file reading though. Look at BufferedReader tutorials. It's not too hard to figure out.