gpt4 book ai didi

java - 将结果集中的值写入文本文件(.txt 文件)

转载 作者:行者123 更新时间:2023-12-02 00:59:17 25 4
gpt4 key购买 nike

请帮助我完成以下代码,因为我想将结果集中的值写入 txt 文件

代码

while (rs.next()){
FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);

out.write(Integer.toString(rs.getInt("SBL_PRODUCT_ID")) + ", ");
out.write(Integer.toString(rs.getInt("SBL_TARIFF_ID")) + ", ");
out.write(rs.getString("PRODUCT_DESCRIPTION") + ", ");
out.write(rs.getString("SERVICE_TYPE") + ", ");
out.write(Integer.toString(rs.getInt("MARKET_CLASS")) + ", ");
out.write(rs.getString("ENTITY_TYPE") + ", ");
out.newLine();
/*out.write(System.getProperty("line.separator"));*/

System.out.println("Completed writing into text file");
out.close();
}

txt 文件中所需的输出

4087, 98, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL

4087, 99, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL

4087, 100, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL

4087, 101, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL

我得到的当前输出只是一行,也是结果集中的最后一个值,即下面

电流输出

4087, 101, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL

请帮我解决这个问题:(

最佳答案

每次创建文件编写器时,您都会覆盖该文件。将其更改为:

FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);
while (rs.next()) {
out.write(Integer.toString(rs.getInt("SBL_PRODUCT_ID")) + ", ");
out.write(Integer.toString(rs.getInt("SBL_TARIFF_ID")) + ", ");
out.write(rs.getString("PRODUCT_DESCRIPTION") + ", ");
out.write(rs.getString("SERVICE_TYPE") + ", ");
out.write(Integer.toString(rs.getInt("MARKET_CLASS")) + ", ");
out.write(rs.getString("ENTITY_TYPE") + ", ");
out.newLine();
/*out.write(System.getProperty("line.separator"));*/
}
System.out.println("Completed writing into text file");
out.close();

或者,您可以在 FileWriter 中设置 append 标志:

FileWriter fstream = new FileWriter(file, true);

尽管这不如仅打开一次文件那么有效。

关于java - 将结果集中的值写入文本文件(.txt 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57783018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com