'Question about java IO FileInputStream written by myself

I wrote an example of FileInputStream,But I found that if I use read() directly in the loop, there will be a problem with the printed data, and it cannot be displayed completely. After using the int variable to receive the return value of read(), it can be displayed normally here. Why? code:

    String path = "d:/hello.txt";
    FileInputStream fileInputStream = null;
    int readData = 0;
    try {
        fileInputStream = new FileInputStream(path);
        while ((readData = fileInputStream.read()) != -1) {
            System.out.print((char) readData);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            fileInputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source