Java.io.FileInputStream Class in Java
FileInputStream is useful to read data from a file in the form of sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
Constructor and Description
- FileInputStream(File file) :Creates an input file stream to read from the specified File object.
- FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read from the specified file descriptor.
- FileInputStream(String name) :Creates an input file stream to read from a file with the specified name.
Important Methods:
- int read() : Reads a byte of data from this input stream.
Syntax: public int read() throws IOException Returns: the next byte of data, or -1 if the end of the file is reached. Throws: IOException
- int read(byte[] b) :Reads up to b.length bytes of data from this input stream into an array of bytes.
Syntax:public int read(byte[] b) throws IOException Parameters: b - the buffer into which the data is read. Returns: the total number of bytes read into the buffer, or -1. Throws: IOException
- int read(byte[] b, int off, int len) : Reads up to len bytes of data from this input stream into an array of bytes.
Syntax:public int read(byte[] b, int off, int len) throws IOException Parameters: b - the buffer into which the data is read. off - the start offset in the destination array b len - the maximum number of bytes read. Returns: the total number of bytes read into the buffer, or -1 Throws: NullPointerException IndexOutOfBoundsException
- long skip(long n) : Skips over and discards n bytes of data from the input stream.
Syntax:public long skip(long n) throws IOException Parameters: n - the number of bytes to be skipped. Returns: the actual number of bytes skipped. Throws: IOException
- int available() : Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream .
Syntax:public int available() throws IOException Returns: an estimate of the number of remaining bytes that can be read Throws: IOException
- void close() : Closes this file input stream and releases any system resources associated with the stream.
Syntax:public void close() throws IOException Specified by: close in interface AutoCloseable Overrides: close in class InputStream Throws: IOException
- FileDescriptor getFD() :Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
Syntax :public final FileDescriptor getFD() throws IOException Returns: the file descriptor object associated with this stream. Throws: IOException
- FileChannel getChannel() :Returns the unique FileChannel object associated with this file input stream.
Syntax :public FileChannel getChannel() Returns: the file channel associated with this file input stream
- void finalize() :Ensures that the close method of this file input stream is called when there are no more references to it.
Syntax :protected void finalize() throws IOException Overrides: finalize in class Object Throws: IOException
Steps to read data from a file using FileInputStream
- First , attach file to a FileInputStream as shown here:
FileInputStream fileInputStream =new FileInputStream(“file.txt”);
- This will enable us to read data from the file. Then , to read data from the file, we should read data from the FileInputStream as;
ch=fileInputStream.read();
When there is no more data available to read further , the read() method returns -1;
- Then we should attach the monitor to output stream. For displaying the data, we can use System.out.print.
System.out.print(ch);
Implementation:
import java.io.*; //Java program demonstrating FileInputStream class ReadFile { public static void main(String args[]) throws IOException { //attach the file to FileInputStream FileInputStream fin= new FileInputStream( "file1.txt" ); //illustrating getChannel() method System.out.println(fin.getChannel()); //illustrating getFD() method System.out.println(fin.getFD()); //illustrating available method System.out.println( "Number of remaining bytes:" +fin.available()); //illustrating skip method /*Original File content: * This is my first line * This is my second line*/ fin.skip( 4 ); System.out.println( "FileContents :" ); //read characters from FileInputStream and write them int ch; while ((ch=fin.read())!=- 1 ) System.out.print(( char )ch); //close the file fin.close(); } } |
Output:
sun.nio.ch.FileChannelImpl@1540e19d java.io.FileDescriptor@677327b6 Number of remaining bytes:45 FileContents : is my first line This is my second line
BufferedInputStream can be used to read a buffer full of data at a time from a file. This improves the speed of execution .
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.BitSet class methods in Java with Examples | Set 2