将结果集中的值写入文本文件(.txt 文件)
To write the value from resultset to text file (.txt file)
请帮助我处理以下代码,因为我想将结果集中的值写入 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
请帮我解决这个问题:(
打开文件一次,将内容写入其中,然后在写入所有内容后关闭,例如
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");
}
查看The try-with-resources Statement了解更多详情
每次创建文件编写器时,您都会覆盖文件。将其更改为:
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);
尽管这不如只打开文件一次有效。
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();
}
}
请帮助我处理以下代码,因为我想将结果集中的值写入 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
请帮我解决这个问题:(
打开文件一次,将内容写入其中,然后在写入所有内容后关闭,例如
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");
}
查看The try-with-resources Statement了解更多详情
每次创建文件编写器时,您都会覆盖文件。将其更改为:
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);
尽管这不如只打开文件一次有效。
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();
}
}