15

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.

orbfish
  • 6,349
  • 12
  • 50
  • 71

5 Answers5

5

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.

maximdim
  • 7,449
  • 3
  • 30
  • 43
  • 1
    As @Tudor pointed out, File doesn't close() - so does the actual file close when you use Scanner this way, or is the resource left hanging? – orbfish Jan 26 '12 at 16:39
  • 3
    Yes, if you pass File into Scanner constructor it is used to open stream which would be closed upon calling Scanner.close() method. Doesn't matter what constructor you use if you call close() properly (e.g. from try/finally) then it would be properly closed. – maximdim Jan 26 '12 at 18:10
2

The File class has no close() method because it only abstracts a disk file. It is not an input stream to the file, so there is nothing to close.

Tudor
  • 58,972
  • 12
  • 89
  • 138
  • Yes, I know File isn't Closeable. So, do you leave a resource hanging around (as suggested by the link in the question) or not? – orbfish Jan 26 '12 at 16:38
  • 2
    You don't leave anything hanging around. The File class is just an information holder for a physical file. It's not a file handle. – Tudor Jan 26 '12 at 16:43
1

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();
Jim Gao
  • 109
  • 1
  • 4
1

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).

Peter Perháč
  • 19,614
  • 21
  • 116
  • 148
-4

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.

bdabmxican
  • 41
  • 5
  • This does not answer my question at all. Please read the question if you're going to answer it. Note: there's a lot of debate out there as to whether to use Scanner or straight BufferedReader. Both are provided by Java, I'm trying to figure out best practice of using it, not decide whether to use it. – orbfish Jan 26 '12 at 16:37
  • Yes, I've read it, didn't answer my question, which you would know if you read my question! – orbfish Feb 21 '12 at 19:01
  • it would be better if the reason for the preference was stated as well – sachsom Apr 30 '21 at 20:49