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

时间:2015-07-03 05:38:10

标签: java text-files multiple-resultsets

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

请帮助我:(

3 个答案:

答案 0 :(得分: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");
}

有关详细信息,请查看The try-with-resources Statement

答案 1 :(得分: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);

虽然这不如打开文件一样有效。

答案 2 :(得分: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();
}

}