About FileReader from learn intermediate java course

    FileReader reader = new FileReader(path);
    int counter = 0;
    while ((counter = reader.read()) != -1) {    

Someone explain me how this while loop.I really have no idea:(


reader.read() read chars one at a time and returns “The character read or -1 if the end of the stream has been reached” (from the Java docs). Every time it reads a character, it moves its internal pointer to the next char so that the next time read() is called it will read the next char and not the same one all over again.

So counter should keep changing because a new char is read every time the loop runs. But when the FileReader reaches the end of the file, it will return -1 to signal that the end has been reached. The while loop basically keeps running until it receives this signal, then it stops.