java 字符串写入文件

package com.race.test;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.race.RaceJwFsStandardApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;



@RunWith(SpringRunner.class)
@SpringBootTest(classes = RaceJwFsStandardApplication.class)
@Slf4j
public class TestClass {


    @Test
    public void fstest1()  {
        String jsonFilePath1 = "C:\\Users\\pc\\Desktop\\1\\aa\\rbmp\\json.txt";
        String jsonFilePath2 = "C:\\Users\\pc\\Desktop\\1\\aa\\rbmp\\json1.txt";
        String outputFilePath = "C:\\Users\\pc\\Desktop\\1\\aa\\rbmp\\generated_sql.txt";

        try (BufferedReader reader1 = new BufferedReader(new FileReader(jsonFilePath1));
             BufferedReader reader2 = new BufferedReader(new FileReader(jsonFilePath2));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            // 读取第一个JSON文件内容

            StringBuilder jsonContent1 = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new FileReader(jsonFilePath1))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    jsonContent1.append(line);
                }
            }
            if (jsonContent1.length() > 0 && jsonContent1.charAt(0) == '\uFEFF') {
                jsonContent1.deleteCharAt(0);
            }

            JSONObject jsonObject1 = JSONObject.parseObject(jsonContent1.toString());
            JSONArray rowsArray = jsonObject1.getJSONArray("rows");

            // 读取第二个JSON文件内容
            int i8=998;
            String sqlStatement ="";
            for (int i = 0; i < 100; i++) {
                JSONObject row = rowsArray.getJSONObject(i);
                String cardNo = row.getString("cardNo");
                String id = reader2.readLine();
                sqlStatement = sqlStatement+"update std_pop_real set card_no='" + cardNo + "' where id='" + id + "';";
            }


            writer.write(sqlStatement);
            writer.newLine();
            System.out.println("SQL statements have been generated and saved to " + outputFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值