Java FileInputStream

FileInputStream is a class in Java that is used for reading data from a file as a stream of bytes. It is a subclass of InputStream, which is an abstract class that provides a standard way of reading data from different sources in the form of bytes.

Here's an example of using the FileInputStream class to read data from a file:

import java.io.*;

public class FileInputStreamExample {
    public static void main(String[] args) {
        try {
            FileInputStream input = new FileInputStream("file.txt");
            int data = input.read();
            while (data != -1) {
                System.out.print((char) data);
                data = input.read();
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Sourc‮gi.www:e‬iftidea.com

In this example, we create a FileInputStream object and pass the name of the file we want to read from as a parameter. We then read data from the file using the read method of the FileInputStream object. The read method returns the next byte of data from the file as an integer, or -1 if the end of the file has been reached. We use a while loop to read the data one byte at a time and print it to the console.

It's important to note that the read method of the FileInputStream class reads a single byte at a time, so it is not very efficient when reading large amounts of data. To read data more efficiently, you can use the BufferedInputStream class, which reads data into an internal buffer and provides methods for reading data in larger chunks.

Overall, the FileInputStream class provides a flexible and efficient way of reading data from a file in Java. By using the various methods provided by FileInputStream, you can read data from files in a variety of different ways depending on your specific needs.