Home >Java >javaTutorial >How to read into text database in java

How to read into text database in java

下次还敢
下次还敢Original
2024-04-21 02:51:42642browse

Java reading methods for text databases include: file stream method (using FileReader and BufferedReader), Scanner class method (using Scanner), database connection method (using JDBC API).

How to read into text database in java

Java method of reading text database

1. Use file stream

Text files can be read line by line using the java.io.FileReader and java.io.BufferedReader classes.

<code class="java">import java.io.BufferedReader;
import java.io.FileReader;

public class ReadTextFile {

    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("text.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                // 处理每一行文本
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}</code>

2. Use the Scanner class

java.util.Scanner class provides a higher level text reading function that can automatically parse different types of data.

<code class="java">import java.util.Scanner;

public class ReadTextFileWithScanner {

    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(new File("text.txt"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                // 处理每一行文本
            }
            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}</code>

3. Use database connection

If the text file is stored in the database, you can use the JDBC (Java Database Connectivity) API to read the data.

<code class="java">import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ReadTextDatabase {

    public static void main(String[] args) {
        try {
            // 连接到数据库
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "username", "password");
            
            // 创建查询语句
            String query = "SELECT * FROM text_table";
            
            // 执行查询
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            
            // 遍历结果集
            while (resultSet.next()) {
                String text = resultSet.getString("text");
                // 处理文本数据
            }
            
            // 关闭连接
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}</code>

The above is the detailed content of How to read into text database in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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