All About Class FileInputStream in Java



FileInputStream is used to manage a  FileInput stream as an input stream for reading data from a File or from a FileDescriptor.FileInputStream class uses actual data files as the basis for an input stream.An object of this class represents a text file which can be accessed in sequential order byte by byte.

The class structure of FileInputStream is given as:

public class java.io.FileInputStream extends java.io.InputStream{
//constructors
public FileInputStream(File file);
//create a FileInputStream object to read from the specified File object. It may throw
//FileNotFoundException if the file is not available.
public FileInputStream(FileDescriptor fdObj);
//create a FileInputStream object to read from the specified FileDescriptor object. It may throw
//SecurityException if a security manager exists,and its checkRead() method is called with the 
//file descriptor to see if the application is allowed to read from the specified FileDescriptor.
//This may result in a SecurityException.
public FileInputStream(String name);
//create a FileInputStream object to read from the specified File object. It may throw
//FileNotFoundException if the file is not available.It may throw
//SecurityException if a security manager exists,and its checkRead() method is called with the 
//file descriptor to see if the application is allowed read access to the file.
//This may result in a SecurityException.
//Methods
public int available();//returns the number of bytes of input currently available for reading.
public int read();//returns an integer representation of the next available byte of input.
public int read(byte b[]);//attempts to read up to b.length bytes into b and returns the actual number
// of the bytes that were successfully read.
public int read(byte b[],int offSet,int length);/attempts to read up to length bytes into b starting 
//at b[offSet] returning the number of bytes successfully read. 
public void close();//closes the input source;further read attempt on a closed input will generate
//an IOException.
protected void finalize();
public final FileDescriptor getFD();
public long skip(long l);//skips over l bytes of input and returning the number of bytes skipped.
}
 
In addition,all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
An Example

class InputStreamTest{
public static void main(String args[])
{
int size;
FileInputStream fin=new FileInputStream("C:\Test\myFile.txt");
size=fin.available();
System.out.println("Total bytes"+size);
for(int i=0;i<size/4;i++)
System.out.println((char)fin.read());
System.out.println("remaining bytes"+fin.available());
System.out.println("remaining 1/4 is displayed :using read(b[]");
byte b[]=new byte[size/4];
if(fin.read(b)!=b.length)
{
System.out.println("File reading error");
}
else
{
String temp=new String(b,0,0,,b.length);//converts bytes to ASCII code
System.out.println(temp);
System.out.println("Still available :"+fin.available());
System.out.println("skipping 1/4: using skip()");
fin.skip(size/4);
System.out.println("file remaining for reading"+fin.available());
}
fin.close();
}
}
 
FileInputStream object can be opened in the following way:

  1. FileInputStream fin;
    fin=new FileInputStream(String name);//name indicates the source file
  2. File myFile;
    FileInputStream fin;
    myFile=new File(String name);
    fin=new FileInputStream(myFile);
All About Class FileInputStream in Java All About Class FileInputStream in Java Reviewed by Animesh Chatterjee on October 09, 2018 Rating: 5

No comments:

Powered by Blogger.