Home > Java > javaTutorial > body text

Use the BufferedReader and BufferedWriter classes to achieve efficient Java file reading and writing

PHPz
Release: 2023-12-28 11:26:26
Original
532 people have browsed it

Use the BufferedReader and BufferedWriter classes to achieve efficient Java file reading and writing

Use the BufferedReader and BufferedWriter classes for efficient Java file reading and writing

Java provides many classes for file reading and writing, among which BufferedReader and BufferedWriter are more commonly used of two classes. Using these two classes can improve the efficiency of file reading and writing.

The BufferedReader class inherits from the Reader class and provides a character input stream with a buffer. It can read multiple characters at a time, reducing the number of accesses to the underlying input stream and improving reading efficiency.

The BufferedWriter class inherits from the Writer class and provides a character output stream with a buffer. Multiple characters can be written at one time, which reduces the number of accesses to the underlying output stream and improves writing efficiency.

The following is a specific example of using BufferedReader and BufferedWriter to read and write files:

import java.io.*;

public class FileReadWriteExample {

    public static void main(String[] args) {
        
        // 源文件路径
        String sourceFile = "source.txt";
        // 目标文件路径
        String targetFile = "target.txt";
        
        try (
            // 创建BufferedReader对象
            BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
            // 创建BufferedWriter对象
            BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile));
        ) {
            // 读取源文件的一行数据
            String line;
            while ((line = reader.readLine()) != null) {
                // 将读取到的数据写入目标文件
                writer.write(line);
                // 写入换行符
                writer.newLine();
            }
            
            System.out.println("文件读写完成。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above code, the source file path and the target file path are first defined. Then, the BufferedReader and BufferedWriter objects are created in the try-with-resources statement, and the corresponding file read and write stream objects are passed in. In the loop, use the readLine() method to read the data of the source file line by line, use the write() method to write the data to the target file, and then use the newLine() method to write newline characters. Finally, the BufferedReader and BufferedWriter objects are closed in the finally block. If an exception occurs while reading or writing the file, the catch block will capture and print the exception information.

By using the BufferedReader and BufferedWriter classes, the efficiency of file reading and writing can be effectively improved. Because these two classes use buffers, they can read and write more data at one time, reducing the number of accesses to the underlying input and output streams, thereby increasing the reading and writing speed. When processing large files, using BufferedReader and BufferedWriter can significantly reduce the number of IO operations and improve program performance.

Summary:
Using the BufferedReader and BufferedWriter classes can improve the efficiency of Java file reading and writing. Through the use of buffers, multiple characters can be read and written at one time, which reduces the number of accesses to the underlying input and output streams and improves the efficiency of IO operations. In actual development, especially when processing large files, using these two classes can significantly improve the performance of the program.

The above is the detailed content of Use the BufferedReader and BufferedWriter classes to achieve efficient Java file reading and writing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template