String fileName;
Scanner nameReader = new Scanner(System.in);
System.out.println("Enter a file name");
fileName = nameReader.nextLine();
Scanner scan = new Scanner(new FileReader(fileName));
For the code above, my professor told me that the FileReader esentially reads in data from the file to the Scanner's buffer. Then, we can access the data from the buffer using the next method, nextLine method, etc. My question is, does the last line in the code provided above automatically and immediately fill scan's buffer with data read in from the file? I was confused by his statement, as I thought we would have to use some method for this to occur.
CodePudding user response:
Honestly, I don't know if I understand your question well But if you want to use the scanner to read from a file, the following code reads and prints all the lines in the file in the desired path
try {
String path = "your file path";
FileReader fileReader = new FileReader(path);
Scanner input = new Scanner(fileReader);
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
// stuff
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
CodePudding user response:
For the code above, my professor told me that the FileReader esentially reads in data from the file to the Scanner's buffer.
That suggests a slight misunderstanding. The FileReader
has no particular knowledge of the Scanner
, and cannot proactively do anything to or with it. The agency goes in the other direction: the Scanner
uses the FileReader
to fill its own buffer.
My question is, does the last line in the code provided above automatically and immediately fill scan's buffer with data read in from the file? I was confused by his statement, as I thought we would have to use some method for this to occur.
Probably not, but that's up to the Scanner
. Do remember that one of its constructors will run, and it is entirely reasonable to consider constructors a special kind of method. In that sense, you are using a method of the Scanner
, and it has an opportunity to eagerly fill the Scanner
's buffer.
In any case, there is no public method whose specific purpose is to fill the buffer. Whether or not the constructor fills the buffer, the methods that scan data will (re)fill the buffer at need.
CodePudding user response:
For the code above, my professor told me that the
FileReader
essentially reads in data from the file to theScanner
's buffer.
I think you misheard / misremembered your professor. Or maybe, they "mispoke".
A more accurate characterization is that the (second) Scanner
reads from the file by calling methods on the FileReader
. The FileReader
has no knowledge of the Scanner
that uses it ... yet alone that the Scanner
has (or may have) an internal buffer of some kind.
It is important to note that we have two Scanner
objects in the example. They respectively getting information from two different sources. One reads from standard input, and the the second one from a file.
My question is, does the last line in the code provided above automatically and immediately fill scan's buffer with data read in from the file?
The answer is:
In practice, No. Current implementations of
Scanner
will not perform the first read from theFileReader
until your application actually calls a method on theScanner
object; e.g. anext
orhasNext
method. The constructor won't do it.In theory it is possible. The javadoc is not crystal clear on this point. It clearly says that a number of "scanning operations" may block waiting for data, which implies that they may read data from the underlying data source. But it does NOT say anything about whether the constructors may or may not block.
I imagine that a vast amount of real world Java code has been written on the (IMO) reasonable assumption that a
Scanner
constructor will not try to preread data. There is zero chance thatScanner
will be changed to do this.