Java File Stream Reading with InputStream

In JAVA, you can use InputStream to read the content of a file stream directly. Here is a simple example:

import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileInputStream {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("path/to/your/file.txt");
            int data;
            while ((data = fis.read()) != -1) {
                System.out.print((char) data);
            }
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, we used FileInputStream to open the file stream, then used the read() method to read the file content byte by byte, and outputted it on the console. Finally, we closed the file stream to release resources.

Please make sure to replace “path/to/your/file.txt” in the code with the actual file path.

bannerAds