But it still reads the hole file.
If the code you posted is the code you're running, then the file has 10 lines or less. On the off chance that I'm stupid and failed to properly compile and run your code in my head, I tested it here and it worked perfectly.