Java 读取TXT文件,写入TXT

本文介绍了如何使用Java读取和写入D盘的123.json文件,展示了基本的BufferedReader和OutputStreamWriter操作。重点在于文件操作的实践和核心代码理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;

    public class Test {
        public static void main(String[] args) {
            //读取文件
            readFileContent("D:\\1234.json");
            //写入文件
            String txt="测试写入文件";
            WriteFile(txt);
        }
        
        public static String readFileContent(String fileName) {
            File file = new File(fileName);
            BufferedReader reader = null;
            StringBuffer sbf = new StringBuffer();
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    sbf.append(tempStr);
                }
                reader.close();
                return sbf.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            return sbf.toString();
        }
        
        public static void WriteFile(String fileString){
            try {
                OutputStreamWriter     osw = new OutputStreamWriter(new FileOutputStream("D:/123.json"),"UTF-8");
                osw.write(fileString.toString());
                osw.flush();//清空缓冲区,强制输出数据
                osw.close();//关闭输出流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 

要将一个txt文件中的数据写入数据库,你需要完成以下步骤: 1. 创建数据库连接:使用DriverManager类的getConnection()方法创建一个数据库连接。此方法需要数据库的URL、用户名和密码等参数。 2. 打开txt文件:使用Java I/O操作打开txt文件,并读取其中的数据。 3. 将数据插入到数据库:使用PreparedStatement对象将读取的数据插入到数据库中。在插入数据之前,你需要为PreparedStatement对象设置SQL语句中的占位符(使用问号表示)并设置对应的参数。 4. 关闭连接:在完成所有操作之后,记得关闭数据库连接。 下面是一个简单的Java程序,演示了如何将txt文件中的数据写入MySQL数据库: ```java import java.io.BufferedReader; import java.io.FileReader; import java.sql.*; public class Main { public static void main(String[] args) { try { // 加载MySQL JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 创建数据库连接 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 打开txt文件 BufferedReader br = new BufferedReader(new FileReader("data.txt")); // 创建插入数据的PreparedStatement对象 String sql = "INSERT INTO mytable(name, age) VALUES(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); // 逐行读取txt文件并插入到数据库中 String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); String name = parts[0]; int age = Integer.parseInt(parts[1]); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.executeUpdate(); } // 关闭连接 pstmt.close(); conn.close(); br.close(); } catch (Exception ex) { ex.printStackTrace(); } } } ``` 在上面的代码中,你需要将数据库URL、用户名和密码替换为你自己的信息。此外,你需要将data.txt文件放在程序所在的目录下,并按照逗号分隔每一行的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值