简体   繁体   English

将文件或FileReader与扫描仪一起使用?

[英]Use File or FileReader with Scanner?

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: 因此, Oracle I / O教程使用Scanner打开一个文本文件,如下所示:

new Scanner(BufferedReader(FileReader("xanadu.txt")));

But the Javadoc opens a text file with Scanner like this: 但是Javadoc用Scanner打开一个文本文件,如下所示:

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")); 编辑:我也见过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. 谢谢。

You could look at implementation of Scanner (JDK is shipped with source code). 您可以查看Scanner的实现(JDK随附源代码)。 There is a close() method in Scanner class as well. Scanner类中也有一个close()方法。 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. 基本上,您列出的两种方法对于读取小文件的用例都是相同的 - 只是不要忘记最后调用close()。

The File class has no close() method because it only abstracts a disk file. File类没有close()方法,因为它只抽象磁盘文件。 It is not an input stream to the file, so there is nothing to close. 它不是文件的输入流,因此无需关闭。

Yes you can do that. 是的,你可以这样做。

Basically you do: 基本上你做:

Scanner file = new Scanner(new FileReader("file.txt"));

To read a String: 要读取字符串:

String s = file.next();

When you are done with the file, do 完成文件后,执行

file.close();

Horses for courses. 马匹课程。 From the Scanner javadocs, a Scanner is "A simple text scanner which can parse primitive types and strings using regular expressions ." 在Scanner javadocs中,Scanner是“一个简单的文本扫描程序,可以使用正则表达式解析基本类型和字符串。” 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. 因此,我对您的问题的看法是:使用哪种方法无关紧要,File的简单选项与Oracle教程中的选项一样好。 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). 基础Readable将是close()d(如果它是一个Closeable,它将是,如果你使用Scanner(File)构造函数),所以只要你关闭()你的Scanner对象你就不必担心(或使用try-with-resources)。

There are multiple ways to construct a Scanner object. 有多种方法可以构建Scanner对象。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html 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. 我个人甚至不会使用Scanner进行文件读取。 Look at BufferedReader tutorials. 查看BufferedReader教程。 It's not too hard to figure out. 弄清楚并不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM