Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Custom Serialization

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.