Which Method Can You Use to Find Out the Number of the Bytes in a File Using Inputstream?
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 ii 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 every bit disk files, devices, other programs, and memory arrays. Several means to read the contents of a file using InputStream in Java accept been discussed beneath.
Suppose the text file is stored in path "/ Users/mayanksolanki/Desktop/Folder/exam.txt " and has content as below:
Approaches:
- Using Apache control IO libraries
- Using readline() method of BufferedReaderClass
- Using read() method of InputStreamClass
Let us depict and implement the methods one by ane 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 every bit shown beneath:
Coffee
import
java.util.*;
import
org.apache.eatables.io.IOUtils;
grade
GFG {
public
static
void
main(String[] args)
{
File file =
new
file(
"/Users/mayanksolanki/Desktop/Folder/test.txt"
);
endeavor
{
FileInputStream input
=
new
FileInputStream(file);
String contents = IOUtils.toString(input);
System.out.println(contents);
}
catch
(IOException due east) {
e.printStackTrace();
}
}
}
Output:
Method 2: BufferedReader's readLine() method
Concept Used: Using inbuilt readLine() role : The readLine() method of BufferedReader class in Java is used to read one line text at a fourth dimension. The end of a line is to exist understood by '\r' or '\northward' or EOF.
Hither is the implementation of r eadLine() method:
Coffee
import
coffee.io.*;
course
GFG {
public
static
void
main(String[] args)
{
File file =
new
File(
"/Users/mayanksolanki/Desktop/Binder/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 information 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 programme.
Java
import
java.io.*;
class
GFG {
public
static
void
principal(String[] args)
{
File file =
new
File(
"/Users/mayanksolanki/Desktop/Binder/examination.txt"
);
try
(FileInputStream input
=
new
FileInputStream(file)) {
int
character;
while
((character = input.read()) != -
1
) {
System.out.impress((
char
)graphic symbol);
}
}
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 so be decoded into characters. Refer to the below example.
Java
import
coffee.nio.charset.StandardCharsets;
import
java.io.*;
course
GFG {
public
static
void
principal(Cord[] args)
{
File file =
new
File(
"/Users/mayanksolanki/Desktop/Folder/test.txt"
);
endeavour
{
(FileInputStream input
=
new
FileInputStream(file))
byte
bytes[]
=
new
byte
[(
int
)file.length()];
input.read(bytes);
String content
=
new
Cord(bytes, StandardCharsets.UTF_8);
Organisation.out.println(content);
}
take hold of
(Exception e) {
east.printStackTrace();
}
}
}
Output
The output of all the higher up programs is the aforementioned.
carrickshmarch1949.blogspot.com
Source: https://www.geeksforgeeks.org/implement-how-to-load-file-as-inputstream-in-java/
0 Response to "Which Method Can You Use to Find Out the Number of the Bytes in a File Using Inputstream?"
Post a Comment