Chatbox

Các bạn vui lòng dùng từ ngữ lịch sự và có văn hóa,sử dụng Tiếng Việt có dấu chuẩn. Chúc các bạn vui vẻ!
14/10/2021 14:10 # 1
buiducduong
Cấp độ: 22 - Kỹ năng: 1

Kinh nghiệm: 7/220 (3%)
Kĩ năng: 0/10 (0%)
Ngày gia nhập: 25/09/2020
Bài gởi: 2317
Được cảm ơn: 0
Java BufferedReader: How to Read File in Java with Example


How to read a file in Java?

Java provides several mechanisms to read from File. The most useful package that is provided for this is the java.io.Reader. This class contains the Class Java BufferedReader under package java.io.BufferedReader

What is BufferedReader in Java?

BufferedReader is a Java class to reads the text from an Input stream (like a file) by buffering characters that seamlessly reads characters, arrays or lines. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

It is therefore advisable to wrap BufferedReader in Java around any Reader whose read() operations may be costly, such as java FileReaders and InputStreamReaders. A typical usage would involve passing the file path to the BufferedReader in Java as follows:

objReader = new BufferedReader(new FileReader("D:\DukesDiary.txt"));
//Assuming you have a text file in D drive

This basically loads your file in the objReader.Now, you will need to iterate through the contents of the file and print it.

The while loop in the below code will read the file until it has reached the end of file

while ((strCurrentLine = objReader.readLine()) != null) {
    System.out.println(strCurrentLine);
}

strCurrentLine reads the current line and the Java readLine function objReader.readLine() returns a string. Hence, the loop will iterate until it’s not null.

BufferedReader Example:

Below code is a Java BufferedReader example which shows the complete implementation:

 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {

 public static void main(String[] args) {
  BufferedReader objReader = null;
  try {
   String strCurrentLine;

   objReader = new BufferedReader(new FileReader("D:\\DukesDiary.txt"));

   while ((strCurrentLine = objReader.readLine()) != null) {

    System.out.println(strCurrentLine);
   }

  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   try {
    if (objReader != null)
     objReader.close();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }
 }
}

Note:

The above code has some very important handlings especially in the finally block of the code.

This code will ensure that the memory management is done efficiently and the objReader.close() method is called that releases the memory.

BufferedReader JDK7 Example:

Below is the example of Java Read Files using BufferedReader class

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample_jdk7 {

 private static final String FILENAME = "D:\\DukesDiary.txt";

 public static void main(String[] args) {

  try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {

   String strCurrentLine;

   while ((strCurrentLine = br.readLine()) != null) {
    System.out.println(strCurrentLine);
   }

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}



 
Copyright© Đại học Duy Tân 2010 - 2024