1

请在下面的代码上帮助我,因为我想将结果集中的值写入 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

请帮助我:(

4

3 回答 3

1

打开文件一次,将内容写入其中,然后在写入所有内容后关闭,例如

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");
}

查看try-with-resources 声明以获取更多详细信息

于 2015-07-03T05:41:52.843 回答
1

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

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();

或者,您可以append在您的FileWriter:

FileWriter fstream = new FileWriter(file, true);

虽然这不如只打开一次文件那么有效。

于 2015-07-03T05:41:55.850 回答
0

来自 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();
}

}

于 2017-04-03T08:10:18.037 回答