Home > Java > javaTutorial > body text

Implement basic file reading and writing functions through the FileReader and FileWriter classes in Java

王林
Release: 2023-12-28 10:39:03
Original
1473 people have browsed it

Implement basic file reading and writing functions through the FileReader and FileWriter classes in Java

Use FileReader and FileWriter classes to implement simple Java file reading and writing

File reading and writing is one of the very common operations in daily programming. Java provides a variety of Classes and methods for file reading and writing. Among them, FileReader and FileWriter are two commonly used classes for reading and writing text files.

The FileReader class is used to read text files. The file content can be read by characters or by character arrays. The FileWriter class is used to write text files. The file content can be written by characters or by character arrays.

The following uses specific code examples to demonstrate how to use the FileReader and FileWriter classes to read and write text files.

First, we create a text file named "test.txt" with the following content:

Hello, FileReader and FileWriter!
Copy after login
Copy after login

Next, we use the FileReader class to read the content of the file and output it to the control On the stage:

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

public class FileReaderExample {
    public static void main(String[] args) {
        try {
            // 创建FileReader对象
            FileReader reader = new FileReader("test.txt");
            
            // 读取文件内容
            int data;
            while ((data = reader.read()) != -1) {
                System.out.print((char) data);
            }
            
            // 关闭文件流
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Run the above code, the console output is:

Hello, FileReader and FileWriter!
Copy after login
Copy after login

Next, we use the FileWriter class to write a piece of text to the file:

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample {
    public static void main(String[] args) {
        try {
            // 创建FileWriter对象
            FileWriter writer = new FileWriter("output.txt");
            
            // 写入内容到文件
            writer.write("Hello, FileWriter!");
            
            // 关闭文件流
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

After running the above code, a text file named "output.txt" will be generated in the current directory with the content:

Hello, FileWriter!
Copy after login

The above is a sample code for using the FileReader and FileWriter classes to implement simple Java file reading and writing. . Through these two classes, we can easily read and write text files and realize file reading and writing operations. Of course, in actual applications, we can also use other more advanced classes and methods to read and write files to meet more complex needs.

The above is the detailed content of Implement basic file reading and writing functions through the FileReader and FileWriter classes in Java. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!