![]() |
VOOZH | about |
The FileInputStream class in Java is used to read data from a file in the form of bytes. Itβs ideal for reading binary data such as images or audio files. For reading text files, itβs better to use FileReader.
The FileInputStream class extends the InputStream class, which means it inherits methods for reading raw byte data from files.
public class FileInputStream extends InputStream
Example: FileInputStream class to read data from file.
Output:
Creates an input file stream to read from a file with the specified name.
FileInputStream fi = new FileInputStream("example.txt");
Creates an input file stream to read from the specified File object.
File f = new File("example.txt");
FileInputStream fi = new FileInputStream(f);
Creates an input file stream to read from the specified file descriptor.
FileDescriptor fd = FileDescriptor.in;
FileInputStream fi = new FileInputStream(fd);
Create a file named file.txt in your project directory with the following content:
this is my first code
this is my second code
Output:
| Methods | Action Performed |
|---|---|
| available() | Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream. |
| close() | Closes this file input stream and releases any system resources associated with the stream. |
| finalize() | Ensures that the close method of this file input stream is called when there are no more references to it. |
| getChannel() | Returns the unique FileChannel object associated with this file input stream. |
| getFD() | Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream. |
| read() | Reads a byte of data from this input stream |
| read(byte[] b) | Reads up to b.length bytes of data from this input stream into an array of bytes. |
| read(byte[] b, int off, int len) | Reads up to len bytes of data from this input stream into an array of bytes. |
| skip() | Skips over and discards n bytes of data from the input stream |