java 将结果集中的值写入文本文件(.txt 文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31199249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To write the value from resultset to text file (.txt file)
提问by Neethu Shaji
Please help me on the below code as i want to write the values from the resultset to a txt file
请帮助我处理以下代码,因为我想将结果集中的值写入 txt 文件
Code
代码
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();
}
Required Output in the txt file
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
Current output which i am getting is only one line that too the last value from the resultset ie below
我得到的当前输出只是一行,也是结果集中的最后一个值,即下面
Current output
电流输出
4087, 101, POSTE SIM, Deal [SoHo Flat Tariff Recurring Charge, Prepaid], /service/telco/SIM, 101, DEAL
Kindly help me on this :(
请帮我解决这个问题:(
采纳答案by Mike Laren
Every time you create a file writer you're overwriting the file. Change it to:
每次创建文件编写器时,都会覆盖该文件。将其更改为:
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();
Alternatively, you could had set the append
flag in your FileWriter
:
或者,您可以append
在您的FileWriter
:
FileWriter fstream = new FileWriter(file, true);
although this is not as efficient as opening the file just once.
虽然这不如只打开一次文件那么有效。
回答by MadProgrammer
Open the file once, write the contents to it and THEN close once ALL the content is written, for example
打开文件一次,将内容写入其中,然后在写入所有内容后关闭,例如
try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
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();
}
System.out.println("Completed writing into text file");
}
Have a look at The try-with-resources Statementfor more details
查看try-with-resources 声明以了解更多详细信息
回答by OhadR
来自 GitHub:https: //github.com/OhadR/ohadr.common/blob/master/src/main/java/com/ohadr/common/utils/resultset/ResultSetConverters.java
public static void writeResultSetToWriter(ResultSet resultSet, PrintWriter writer) throws SQLException
{
ResultSetMetaData metadata = resultSet.getMetaData();
int numColumns = metadata.getColumnCount();
int numRows = 0;
while(resultSet.next()) //iterate rows
{
++numRows;
JSONObject obj = new JSONObject(); //extends HashMap
for (int i = 1; i <= numColumns; ++i) //iterate columns
{
String column_name = metadata.getColumnName(i);
obj.put(column_name, resultSet.getObject(column_name));
}
writer.println(obj.toJSONString());
if(numRows % 1000 == 0)
writer.flush();
}
}
}