java导出变量输出到csv

时间:2017-06-06 01:10:51

标签: java file export-to-csv

知道为什么这段代码不能打印空白输出文件??

`BufferedWriter out = new BufferedWriter(new FileWriter(new File(path)));

while (rs.next()) {
                        String id = rs.getString("id");
                        String title = rs.getString("title");
                        String content = rs.getString("introtext");
                        String parsed = Jsoup.parse(content).select("body").text();
                        data.add(id + " " + title + " " + parsed);
                        System.out.println(id + "\t" +title + "\t" + parsed );
                        out.write(rs.getString(id));
                        out.write(rs.getString(title));
                        out.write(rs.getString(parsed));
                    }

                rs.close();
                stmt.close();
                out.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }`

1 个答案:

答案 0 :(得分:0)

您的输出似乎有缺陷,因为您首先获取id,title并进行解析,然后尝试从查询中获取这些值(并且它们可能不存在)。此

out.write(rs.getString(id));
out.write(rs.getString(title));
out.write(rs.getString(parsed));

很可能是

out.write(id);
out.write(title);
out.write(parsed);

你可能也想写一个逗号(在这些字段之间)。