FileInputStream class is a subclass of InputStream abstract class. FileInputStream is used create an input stream, which is used to read byte/bytes from a file.

InputStream class basically used to read the data from source byte by byte.

FileInputStream class basically used to read the data from File byte by byte.

  • Constructors of FileInputStream class

    Commonly used constructor of FileInputStream class are as follows :-

    Constructors Description
    FileInputStream(File file) This constructor creates a FileInputStream object to read a file specified by the File object, which is passed to this constructor as a parameter.
    FileInputStream(String path) This constructor creates a FileInputStream to read a file which is accessed by the path mentioned in the parameters of this constructor.
  • Java FileInputStream class methods

    Some methods for reading FileInputStream

    Methods Description
    available() This method gives the remaining number of bytes available to read from this input stream.
    read() This method reads one byte from this input stream.
    read(byte[] b) This method reads a whole byte array from this input stream.
    close() This method closes this input stream and also frees any resources connected with this input stream.

    Example : read single character using FileInputStream

    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class ReadInput {
        public static void main(String[] args) {
            try {
                FileInputStream fis = new FileInputStream("myfile.txt");
                int i = fis.read();//reading single byte
                System.out.println((char) i);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Output:
    t

    Before running the code, a text file named as "myfile.txt" is required to be created. In this file, write some text.


    Example : reading all the content of file byte by byte using FileInputStream.

    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class ReadInput {
        public static void main(String[] args) {
            try {
                FileInputStream fis = new FileInputStream("myfile.txt");
    
                while(true)
                {
                    int i=fis.read();
                    if(i==-1)
                        break;
                    System.out.print((char)i);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Output:
    this is demo file only for testing .

    Example : Reading all the content of file at once using FileInputStream

    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class FileInput1 {
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("myfile.txt");
            byte[] data=new byte[fis.available()];//available() : method returns number of bytes in stream
            fis.read(data);//read the bytes and put to array
            String content=new String(data); //convert the byte array  to string
            System.out.println(content);
    
    
        }
    }
    

    Output:
    this is demo file only for testing .

What next?

The next topic is FileOutputStream in java

Share this page