Implement how to load File as InputStream in Java
Last Updated :
21 Sep, 2021
Problem Statement: There is a file already existing in which the same file needs to be loaded using the InputStream method.
Concept: While accessing a file either the file is being read or write. here are two streams namely fileInputStream and fileOutputStream
Java Stream is the flow of data from source to destination. OutputStream and InputStream are abstractions over low-level access to data. InputStream is a source for reading data. A stream can have various kinds of sources, such as disk files, devices, other programs, and memory arrays. Several ways to read the contents of a file using InputStream in Java have been discussed below.
Suppose the text file is stored in path “/Users/mayanksolanki/Desktop/Folder/test.txt” and has content as below:
Approaches:
- Using Apache command IO libraries
- Using readline() method of BufferedReaderClass
- Using read() method of InputStreamClass
Let us describe and implement the methods one by one through examples:
Method 1: Using Apache Common IO library
The IOUtils class from Apache Commons IO library contains a toString() method that accepts an InputStream and renders its contents as a string as shown below:
Java
import java.util.*;
import org.apache.commons.io.IOUtils;
class GFG {
public static void main(String[] args)
{
File file = new file(
"/Users/mayanksolanki/Desktop/Folder/test.txt" );
try {
FileInputStream input
= new FileInputStream(file);
String contents = IOUtils.toString(input);
System.out.println(contents);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
|
Output:
Method 2: BufferedReader’s readLine() method
Concept Used: Using inbuilt readLine() function : The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by ‘\r’ or ‘\n’ or EOF.
Here is the implementation of readLine() method:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
File file = new File(
"/Users/mayanksolanki/Desktop/Folder/test.txt" );
try (FileInputStream input
= new FileInputStream(file)) {
StringBuilder content = new StringBuilder();
BufferedReader br = new BufferedReader(
new InputStreamReader(input));
String line;
while ((line = br.readLine()) != null ) {
content.append(line
+ System.lineSeparator());
}
System.out.println(content.toString());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
|
Output:
Method 3: InputStream’s read() method
The read() method of InputStream class, reads a byte of data from the input stream. The next byte of data is returned, or -1 if the end of the file is reached and throws an exception if an I/O error occurs. Refer to the program.
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
File file = new File(
"/Users/mayanksolanki/Desktop/Folder/test.txt" );
try (FileInputStream input
= new FileInputStream(file)) {
int character;
while ((character = input.read()) != - 1 ) {
System.out.print(( char )character);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
There is another overloaded version of read() method that reads the specified length of bytes from the input stream into an array of bytes. The corresponding bytes can then be decoded into characters. Refer to the below example.
Java
import java.nio.charset.StandardCharsets;
import java.io.*;
class GFG {
public static void main(String[] args)
{
File file = new File(
"/Users/mayanksolanki/Desktop/Folder/test.txt" );
try {
(FileInputStream input
= new FileInputStream(file))
byte bytes[]
= new byte [( int )file.length()];
input.read(bytes);
String content
= new String(bytes, StandardCharsets.UTF_8);
System.out.println(content);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output
The output of all the above programs is the same.
Share your thoughts in the comments
Please Login to comment...