我是Java中的數據庫的新手,我試圖導出1表中的數據並將其存儲在文本文件中。目前下面的代碼寫入文本文件,但全部在一行?誰能幫忙?我如何從數據庫獲取數據並將其存儲到文本文件中?
我的代碼
private static String listHeader() {
String output = "Id Priority From Label Subject\n";
output += "== ======== ==== ===== =======\n";
return output;
}
public static String Export_Message_Emails() {
String output = listHeader();
output +="\n";
try {
ResultSet res = stmt.executeQuery("SELECT * from messages ORDER BY ID ASC");
while (res.next()) { // there is a result
output += formatListEntry(res);
output +="\n";
}
} catch (Exception e) {
System.out.println(e);
return null;
}
return output;
}
public void exportCode(String File1){
try {
if ("Messages".equals(nameOfFile)){
fw = new FileWriter(f);
//what needs to be written here
//fw.write(MessageData.listAll());
fw.write(MessageData.Export_Message_Emails());
fw.close();
}
}
我在哪裏編寫System.llineSeparator(); – 2015-02-11 12:59:30
而不是輸出+ =「\ n」寫輸出+ = System.lineSeparator() – 2015-02-11 13:02:09