🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Reading a file in Java can be accomplished using various classes from the java.io package. One of the efficient ways to read a file is by using the BufferedInputStream class, which buffers the input to provide efficient reading of bytes. This blog post will guide you through the process of reading a file using BufferedInputStream.
Table of Contents
- Introduction
- Prerequisites
- Using
BufferedInputStreamto Read a File - Complete Example
- Conclusion
Introduction
BufferedInputStream is a class in the java.io package that provides buffering for an InputStream. It can improve the performance of I/O operations by reducing the number of read operations that are required to fetch data from the underlying input stream. This tutorial will demonstrate how to use BufferedInputStream to read a file efficiently.
Prerequisites
Before you start, ensure you have the following:
- A basic understanding of Java programming
- A Java development environment (JDK and an IDE like IntelliJ IDEA or Eclipse)
Using BufferedInputStream to Read a File
To read a file using BufferedInputStream, you will:
- Create a
FileInputStreamto read the file. - Wrap the
FileInputStreamwith aBufferedInputStream. - Read the file data using the
BufferedInputStream. - Close the streams to release system resources.
Example
The following example demonstrates how to read a file using BufferedInputStream.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileUsingBufferedInputStream {
public static void main(String[] args) {
String fileName = "example.txt";
try (FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
- The
fileNamevariable contains the name of the file to be read. - A
FileInputStreamis created to read the file. - A
BufferedInputStreamis created to wrap theFileInputStream. - A
bytebuffer is used to read chunks of data from the file. - The
readmethod ofBufferedInputStreamreads data into the buffer and returns the number of bytes read. - The bytes read are converted to a
Stringand printed to the console. - The
try-with-resourcesstatement ensures that the streams are closed automatically.
Complete Example
Here is the complete example, including all necessary classes and methods.
example.txt
Create a simple text file named example.txt with the following content:
This is an example file.
It contains multiple lines of text.
This is the third line.
ReadFileUsingBufferedInputStream.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileUsingBufferedInputStream {
public static void main(String[] args) {
String fileName = "example.txt";
try (FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Conclusion
Reading a file in Java using BufferedInputStream is an efficient way to handle file input operations. By buffering the input, BufferedInputStream reduces the number of read operations required, which can improve performance, especially for large files. The example provided demonstrates how to read a file using BufferedInputStream, and you can experiment with the code to gain a deeper understanding of file I/O in Java.
Feel free to modify and experiment with the code examples provided in this tutorial to suit your specific needs. Happy coding!
Comments
Post a Comment
Leave Comment