File Input Stream
Categories: Java 8(JDK1.8)
This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file −
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows −
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.
Sr.No.Method & Description
1. public void close() throws IOException{} - This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2. protected void finalize()throws IOException {} - This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3. public int read(int r)throws IOException{} - This method reads the specified byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it's the end of the file.
4. Public int read(byte[] r) throws IOException{} - This method reads r.length bytes from the input stream into an array. Returns the total number of bytes read. If it is the end of the file, -1 will be returned.
5. public int available() throws IOException{} - Gives the number of bytes that can be read from this file input stream. Returns an int.