Home > Java > javaTutorial > Use Scanner class to implement line-by-line reading and writing operations of Java files

Use Scanner class to implement line-by-line reading and writing operations of Java files

WBOY
Release: 2023-12-28 12:31:13
Original
1056 people have browsed it

Use Scanner class to implement line-by-line reading and writing operations of Java files

Use the Scanner class to implement line-by-line reading and writing of Java files

In Java programs, we often need to read and write files line by line Reading and writing files is one of the common needs. When processing large files, reading and writing line by line can improve the efficiency of the program and avoid memory overflow problems. In this article, we will introduce how to use the Scanner class in Java to implement line-by-line reading and writing operations of files, and provide specific code examples.

First, we need to create a text file to test this functionality. Create a text file named "test.txt" in your project. The file contains several lines of text.

Next, we need to use Java's Scanner class to read the file content line by line. The following is a code example for reading a file:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFileExample {
    public static void main(String[] args) {
        // 指定文件路径
        File file = new File("test.txt");

        try {
            // 使用Scanner类读取文件
            Scanner scanner = new Scanner(file);

            // 逐行读取文件内容
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }

            // 关闭Scanner对象
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

The above code first creates a File object and specifies the file path to be read. Then use the Scanner class to read the file, and read the file content line by line through a while loop. Finally, close the Scanner object and release resources.

We can also use the Scanner class to implement file line-by-line writing operations. The following is a code example for writing to a file:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class WriteFileExample {
    public static void main(String[] args) {
        // 指定文件路径
        File file = new File("output.txt");

        try {
            // 创建FileWriter对象
            FileWriter writer = new FileWriter(file);

            // 使用Scanner类读取用户输入
            Scanner scanner = new Scanner(System.in);

            // 逐行写入文件
            System.out.println("请输入要写入文件的内容:");
            String line = scanner.nextLine();
            while (!line.equals("exit")) {
                writer.write(line + "
");
                line = scanner.nextLine();
            }

            // 关闭FileWriter和Scanner对象
            writer.close();
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

The above code first creates a file named "output.txt" to store the written content. Then a FileWriter object is created to write data to the file. Then use the Scanner class to read user input and write to the file line by line through a while loop. The loop ends when the user enters "exit". Finally close the FileWriter and Scanner objects.

The above is the sample code that uses the Scanner class to read and write Java files line by line. Through these sample codes, you can quickly start using the Scanner class to handle reading and writing files, and make appropriate adjustments and extensions according to actual needs. Hope this article helps you!

The above is the detailed content of Use Scanner class to implement line-by-line reading and writing operations of Java files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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