0

I am trying to write some query results in a csv file.

I thought it was working fine, until I saw the last line of the file after it's been written:

value;nettingNodeData;BLE57385;CVAEngine;BLE;;.4;;BDR;NA;ICE;;RDC;;CVAEngine;;4841263;RDC value;ne

The part where I am writing the file :

public int getLeNodes(String runId, File file) {
    Connection connect = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int lines = 0;

    try {
        connect = newConnection();
        ps = connect.prepareStatement(HIER_NTT.replace("?", runId));
        ps.setFetchSize(1500);
        rs = ps.executeQuery();

        lines += nnpw.writeCore(file, rs);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(rs, ps, connect);
    }
    return lines;
}

public int writeCore(File file, ResultSet rs) throws FileNotFoundException, IOException {
    int count = 0;
    try {
        BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs.next()) {
            for (int col = 1; col <= colCount; col++) {
                try {
                    String val = rs.getString(col);
                    if (rs.wasNull()) {
                        val = "";
                    }
                    output.append(val);
                } catch (ArrayIndexOutOfBoundsException e) {
                    String dec = rs.getBigDecimal(col).toPlainString();
                    System.err.println(String.format("%s %d %s %s %d %s %d", rs.getString(1), col,
                            rsmd.getColumnTypeName(col), file, count, dec, dec.length()));
                    output.append(dec);
                }
                if (col < colCount) {
                    output.append(";");
                }
            }
            output.newLine();
            count++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

And the SQL (HIER_NTT):

select 'value', 'nettingNodeData', 'BLE' || d.deal_numadm, 
'CVAEngine', 'BLE', '', (1-ntt.lgd_cp), '', 'BDR', 'NA', 'ICE', '', 'RDC', '',
'CVAEngine', '', d.ntt_id, 'RDC' from ntt ntt 
join deals d on d.ntt_id = ntt.ntt_id and d.deal_scope='Y' 
join dt_runs r on r.run_id = ntt.run_id 
where r.run_id=? and d.deal_numadm!=0 group by d.deal_numadm, d.deal_nummas, d.ntt_id, ntt.lgd_cp

So, why does my file end abruptly like this?

1 Answer 1

1

You should call output.close() when you are done writing to the file. The missing output is probably still in the buffer when the java process exits

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.