Java Java Data Structures Efficiency! Custom Serialization

Claudio Paladini
Claudio Paladini
11,675 Points

when importing the .txt the readLine() in the while-loop automatically iterates through all of the file's lines.?

In the "importFrom()" method, the while loop uses the "readLine()" method on the "BufferReader" object, "reader", and then assigns it the "String line".

Would it be correct to assume that the readLine() method, when called upon in sequence, iterates/reads each line of the .txt file?

1 Answer

Rob R
Rob R
14,637 Points

Hi Claudio,

Yes, that is correct, because the while loop is basically saying:

("while there are more lines in this java file/the lines aren't empty, assign line to line") {
split the current line
addSong(contents are from the split line)

The while loop will stop once there are no more lines. If you want to play around with seeing what the while loop does, you can add a line in the while loop like this:

while((line = reader.readLine()) != null) {
System.out.println(line); // add this before the String array
String[] args = line.split("\\|");

If for some reason you wanted only the first line of a file, you could just do line = reader.readLine() outside of the while loop and then split the line, etc.

Hope that helps. Let me know if you have other questions.