[Java] How to use FileReader class and BufferedReader class

Programming study diary

November 16, 2020 The usage of FileReader class and BufferedReader class is summarized again.

What is the FileReader class?

It is an API for reading a text file, and is used to perform processing based on the contents of the text file. The characters in the file are read in a stream of characters that can read and write data character by character. The FileReader class has only a read method that reads characters one by one, so if you want to read data in a stream of bytes that can be read and written in byte units, use the FileInputStream class.

Read character by character with read method


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class Main {
 
    public static void main(String[] args) {
        try {
            //Specify the file path as an argument of the File class constructor
            File file = new File("text.txt");
         
            //Exception handling when the file does not exist
            //The exists method returns true if the file exists, false if it does not exist
            if (!file.exists()) {
                System.out.print("File does not exist");
                return;
            }
         
            //Read and display character by character using FileReader class and read method
            FileReader fileReader = new FileReader(file);
            int data;
            //Repeat until the end of the file
            while ((data = fileReader.read()) != -1) {
                //Cast the read character to char type and display the character
                System.out.print((char) data);
            }
         
            //Finally close the file to free resources
            fileReader.close();
         
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}

What is the BufferedReader class?

Like the FileReader class, it is a class that reads text files, and the methods provided are different from the FileReader class. The BufferReader class provides a readline method that reads line by line. The FileReader class reads one character at a time, but if the number of characters is large, it may become inefficient, so decide a certain amount of memory and issue a read command to the OS when it accumulates. This is called buffering.

Read line by line with the readline method


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class Main {
 
    public static void main(String[] args) {
        try {
            //Specify the file path as an argument of the File class constructor
            File file = new File("c:SampleTest.txt");
         
            //Exception handling when the file does not exist
            //The exists method returns true if the file exists, false if it does not exist
            if (!file.exists()) {
                System.out.print("File does not exist");
                return;
            }
         
            //Read and display line by line using the readLine method of the BufferedReader class
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String data;
            //Repeat until the end of the file
            while ((data = bufferedReader.readLine()) != null) {
                System.out.println(data);
            }
         
            //Finally close the file to free resources
            bufferedReader.close();
         
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}

References

Class BufferedReader Class FileReader [Java] Read text files with FileReader and BufferedReader

Recommended Posts

[Java] How to use FileReader class and BufferedReader class
How to use java class
[Java] How to use Calendar class and Date class
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
[Java] How to use the File class
[Java] How to use the HashMap class
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Java] How to use compareTo method of Date class
How to use and apply Java's JFrame / Canvas class
How to use StringBurrer and Arrays.toString.
How to use Java HttpClient (Get)
How to use EventBus3 and ThreadMode
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
How to use the wrapper class
How to use equality and equality (how to use equals)
[Processing × Java] How to use variables
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
How to call and use API in Java (Spring Boot)
Multilingual Locale in Java How to use Locale
How to use OrientJS and OrientDB together
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to output and write files!
How to set up and use kapt
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use substring and substr methods
[Java] Learn how to use Optional correctly
How to use @Builder and @NoArgsConstructor together
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
[Java] How to use static modifiers (What are static final and static import)