Java.io - SPLessons

Java.io FileInputStream

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FileInputStream

Java.io FileInputStream

shape Introduction

Java.io FileInputStream is the class which obtains input bytes from the file system. Java.io FileInputStream reads streams of  bytes from the file, such as data related to image.For reading of characters from file, then file reader is used instead of Java.io FileInputStream.

Class Declaration

shape Declaration

Java.io.FileInputStream   class is declared as follows: Public class FileInputStream extends InputStream

Class Constructors

shape Table

Constructor Description
FileInputStream (File file) A file input stream is created by this constructor and connects to an actual file.
FileInputStream(String name) A file input stream object is created by this constructor and connects to an actual file based on the name provided by the path name in the file system.
FileInputStream(FileDescriptor fdObj) A file input stream object is created by this constructor and used the file descriptor to represent the connection of actual file.

Class Methods

shape Table

Method Description
int available() By using this method it returns the bytes and estimates that remaining bytes have to read from the input stream.
void close() By using this method the file input stream is closed and releases the another resources related to this stream.
int read() From the input stream this method can read bytes of data.
int read(byte[] a) By using this method it reads the data up to a.length bytes from the input stream.
int read(byte[] b, int off, int n) By using this method it reads the data up to 'n' bytes from the input stream into an array of bytes.
protected void finalize() By using this method it can close the input stream at that time close method is called only when there are no more actions to perform.
FileChannel getChannel() Associating with file input stream this method returns unique FileChannel object.
FileDescriptor getFD() In the file system the connection to actual file is represented by the FileDescriptor object which was return by this method being used by this FileInputStream.
long skip(long n) From the input stream this method skips over and throw out 'n' bytes of data.

Inherited Methods

shape Description

From the following classes, methods are inherited to Java.io.FileInputStream
  • Java.io.InputStream
  • Java.io.Object

shape Examples

Usage of FileInputStream.getFD() method. [c]import java.io.FileDescriptor; import java.io.IOException; import java.io.FileInputStream; public class FileInputStream { public static void main(String[] args) throws IOException { FileDescriptor fd = null; FileInputStream fi = null; boolean bl = false; try { // creating new file input stream fi = new FileInputStream("C://test.txt"); // To get file descriptor fd = fi.getFD(); // It will ckecks if the file is valid bl = fd.valid(); // print the file valid are not System.out.println("Valid file: " + bl); } catch (Exception ex) { // if an I/O error occurs ex.printStackTrace(); } finally { // releases all system resources from the file input streams if (fi != null) fi.close(); } } }[/c] Output The result is as follows. [c]Valid file: true.[/c] Usage of void close() method. [c]import java.io.IOException; import java.io.FileInputStream; public class FileInputStream { public static void main(String[] args) throws IOException { FileInputStream fi = null; int i=0; char c; try{ // creating new file input stream fi = new FileInputStream("C://test.txt"); // From file input stream it reads byte i=fi.read(); // converting integer from character c = (char)i; // print the character System.out.println(c); // Then close file input stream fi.close(); System.out.println("Close() invoked"); // It tries to read byte from close file input stream i=fi.read(); c=(char)i; System.out.println(c); }catch(Exception ex) { // if an I/O error occurs System.out.println("IOException: close called before read()"); }finally { // releases all system resources from the streams if(fi!=null) { fi.close(); } } } }[/c] Output The result is as follows. [c]IOException: close called before read(). [/c]

Summary

shape Key Points

  • Java.io FileInputStream is the class which obtains input bytes from the file system.
  • Java.io FileInputStream reads streams of  bytes from the file, such as data related to image.