Search code examples
javavisual-studio-codeinputstream

Problem with Java InputStream's read method not reading the first few bytes


So, i have written a simple program, that receives a text file and is meant to output the characters within the file one by one on separate lines.

Below is the content of the "hello.txt" text file that I'm using:

Hello How Are You?

Here is my code:

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class input_stream {
    public static void main(String args[]) throws FileNotFoundException, IOException {
        InputStream input = new FileInputStream("C:\\Users\\m3hran\\Desktop\\file_test\\hello.txt");

        byte[] byteArray = new byte[1024];

        int ArraysRead = input.read(byteArray);

        input.close();

        System.out.println(ArraysRead);

        for (byte i : byteArray) {
            System.out.println ((char) i);
        }
    }
}

My problem is that my output seems to be skipping the first few bytes for some reason - here is my output:

enter image description here

I was wondering if anyone knows what's causing this issue? Any help would be appreciated!


Solution

  • When running the file at the first time, i got the same result as yours which was post in question, but after enlarging the Terminal and run the file again, the result would be shown correctly, which is as the same as the one in external Terminal: enter image description here

    It seems to be an issue which could be put in github and here's my issue link: Can't show complete java result in Terminal unless resize it.

    Besides, please pay attention to your privacy protection and you may cover the name in your screenshots.