All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Use Bufferedreader In Java

Last Updated : Mar 11, 2024

How To Use Bufferedreader In Java

In this article we will show you the solution of how to use bufferedreader in java, Java's BufferedReader class makes it easy to read text from streams, like files or network connections.

A internal buffer is used to hold the data after it has been read in sections from the input source.

As a result, it is quicker than alternative input techniques and uses fewer system calls.

Create a BufferedReader instance, wrap it over a FileReader or InputStream, as well as read input as a string using the readLine() method.

Close the reader after you've finished reading to free up its resources.

Step By Step Guide On How To Use Bufferedreader In Java :-

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class main {
  private final static String SUBJECT_SEPARATOR = "/";
 private final static String SINGLE_QUOTE = "'";
 public static void main(String[] args) {
  FileInputStream inputStream = null;
  BufferedReader bufferedReader = null;
  FileOutputStream outputStream = null;
  BufferedWriter bufferedWriter = null;
  try {
   inputStream = new FileInputStream("/Users/pk/Desktop/data_normalized.txt");
   outputStream = new FileOutputStream("/Users/pk/Desktop/data_modified.txt");
   bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
   bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
   String codeLine = null;
   String subjectLine = null;
   StringBuilder insertSql = new StringBuilder();
   String subjects[] = null;
   String parentCategory = null;
   String subcategory = null;
   while(true) {
    codeLine = bufferedReader.readLine();
    subjectLine = bufferedReader.readLine();
    if(codeLine == null || subjectLine == null) {
     break;
    }
    codeLine = codeLine.trim();
    subjectLine = subjectLine.trim();
    subjects = subjectLine.split(SUBJECT_SEPARATOR);
    parentCategory = subjects[0].trim();
    subcategory = subjectLine.replace(parentCategory + " / ", "").trim();
    parentCategory = parentCategory.replace("'", "\\'");
    subcategory = subcategory.replace("'", "\\'");
    subjectLine = subjectLine.replace("'", "\\'");
    insertSql.setLength(0);
    insertSql.append("insert into Category (name, fullName, code) values (");
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(subcategory);
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(", ");
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(subjectLine);
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(", ");
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(codeLine);
    insertSql.append(SINGLE_QUOTE);
    insertSql.append(");");
    bufferedWriter.write(insertSql.toString());
    bufferedWriter.newLine();
   }
   bufferedWriter.close();
   bufferedReader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
  1. As you can see, we've written simple Java code to create the "main" class, which has just one function also named "main."
  2. This program's function is to read text file data, process it, and then write the result to another text file.
  3. In order to conduct input as well as output operations in Java, the programme imports a number of classes from the "java.io" package.
  4. These classes consist of InputStreamReader, OutputStreamWriter, FileInputStream, FileOutputStream, BufferedReader, and BufferedWriter.
  5. In order to read from and write to files, a number of variables such as are set up in the "main" method.
  6. Using the FileInputStream class, it then receives data from a file called "data_normalized.txt" located at "/Users/pk/Desktop/" and uses the FileOutputStream class to send the processed data to a file called "data_modified.txt" also at "/Users/pk/Desktop/."
  7. The programme then reads lines from the input file in a while loop until it reaches the last line of the file.
  8. It uses the "/" character to divide each line into its parent category & subcategory.
  9. This data is subsequently inserted in a database table using a SQL query string that is created.
  10. A StringBuilder object is used to build this SQL query.
  11. The BufferedWriter class is then used by the programme to write the query using SQL to the output file.
  12. When an IOException happens when reading or writing, the programme uses the e.printStackTrace() method to print the stack trace of the error.

Conclusion :-

Thus, we have successfully acquired the knowledge necessary to use Java's bufferedreader.

We also discovered that this Java code alters data that has been read from one file before writing it to another.

It prepares a SQL query to put data into a database after splitting the topic line by a separator.

The code handles file I/O exceptions and reads and writes data using a variety of input/output classes.

I hope this article on how to use bufferedreader in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪