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();
}
}
}
java 字符串写入文件
最新推荐文章于 2025-05-16 08:17:19 发布