Methods of FileInputStream
This FileInputStream class gives us the idea to implement the different methods present in the InputStream class.
read()
- read(): This method reads a single byte from the file.
- read(byte[] array): This method reads the bytes from the file and stores them in the specified array.
- 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);}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Line A
Line B
Line C
Line A
Line B
Line C
Line A
Line B
You can also try this code with Online Java Compiler
Run Code
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);}
}
}
You can also try this code with Online Java Compiler
Run Code
Output
L
You can also try this code with Online Java Compiler
Run Code
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
-
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.
-
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.
-
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.