Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java FileInputStream
2.1.
Create a FileInputStream
3.
Methods of FileInputStream
3.1.
read() 
3.2.
available() 
3.3.
skip() 
3.4.
close() 
3.5.
finalize() 
3.6.
getChannel()
3.7.
getFD()
3.8.
mark()
4.
Examples
4.1.
Code 1
4.2.
Code 2
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

File Input Stream class

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In this article, we will be covering the File Input Stream class before jumping to the actual topic. We will brush up on the previous concepts of what is InputStream. Since the InputStream is an abstract class found in the java.io package, which represents an input stream of bytes. The FileInputStream class of the java.io package is used to read bytes from files.

Also see,  Swap Function in Java

Java FileInputStream

The FileInputStream is a class found in the java.io package, which is used to read the data from the files in the byte form.

Create a FileInputStream

To create a file input stream, we first need to import the java.io.FileInputStream package. After importing the package, we can create a file input stream in Java.

  1. Using the path to file
FileInputStream fis = new FileInputStream(stringPath);

2. Using an object of the file

FileInputStream fis = new FileInputStream(File fileObject);
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of FileInputStream

This FileInputStream class gives us the idea to implement the different methods present in the InputStream class.

read() 

  1. read(): This method reads a single byte from the file.
  2. read(byte[] array): This method reads the bytes from the file and stores them in the specified array.
  3. read(byte[] array, int start, int length): This method reads the number of bytes equal to the length from the files and stores them in the specified array beginning from the position start.

available() 

This method returns the number of available bytes present in the file.

skip() 

This method removes and skips the given number of bytes.

close() 

This method closes the FileInputStream. Once this method is called, we cannot use the input stream to read data.

finalize() 

This method keeps the check that the close() method is called.

getChannel()

This method returns the object of FileChanel, associated with the input Stream.

getFD()

This method returns the file descriptor which is associated with the input stream.

mark()

This method marks the position in the input stream up to which data has been read.
 

Also see, Duck Number in Java and Hashcode Method in Java

Examples

The file contents of the input.txt file are: 

Line A
Line B
Line C
Line A
Line B
Line C
Line A
Line B

Code 1

import java.io.FileInputStream;
public class Code1
{
public static void main(String args[])
{    
        try{    
          FileInputStream fin=new FileInputStream("input.txt");    
          int i=0;    
          while((i=fin.read())!=-1){    
           System.out.print((char)i);    
          }    
          fin.close();    
        }catch(Exception e){System.out.println(e);}    
       }    
      }  

Output

Line A
Line B
Line C
Line A
Line B
Line C
Line A
Line B

In this example, we took the FileInputStream and passed a file. After passing the file, we checked certain conditions, and after doing that, we printed the contents of the file.

Code 2

import java.io.FileInputStream;
public class Code2
{
public static void main(String args[])
{   
try
{    
        FileInputStream fin=new FileInputStream("input.txt");    
        int i=fin.read();  
        System.out.print((char)i);    
        fin.close();    
      }
catch(Exception e){System.out.println(e);}    
     }  
}

Output

L

In this example, we passed a text file in the FileInputStream, and then after checking certain conditions, we managed to return the first character of the file.

Try it on online java compiler.

FAQs

  1. What is the use of FileInputStream in Java?
    Java FileInputStream class is used to obtain input bytes from a file. It is used to read input bytes, such as raw bytes from images, audio, or video. 
     
  2. What is the difference between InputStream and OutputStream?
    The fundamental difference between them is that InputStream refers to the classes that receive data from various sources, whereas OutputStream is an abstract class that defines stream byte output.
     
  3. What is the purpose of System class?
    The sole purpose of the System class is to give access to System resources.

Key Takeaways

In this article, we took the topic of the File Input Stream class. We briefly introduced the topic, and we also explained it further. Moreover, we took the examples of how to create the FileInputStrem. We also discussed the different methods which are used in FileInputStream. We took a few coding examples explaining different ways to work on FileInputStream.

I hope that my article might have cleared your doubts. If you want to explore more about these concepts, and learn about File Input/ Output Streams, you can visit 

File Input / Output.

Previous article
nextLine() in Java
Next article
The FileOutputStream Class
Live masterclass