Java OpenCSV Writing Result Set - End of Line / Row

我的梦境 提交于 2019-12-12 15:17:25

问题


I'm trying to export a SQL resultset to a comma delimited csv file using opencsv library. However when I pass the result set to the writeAll function, all works well except for the file that it generates does not separates the rows with a new line and creates a nearly un readable file when opened in notepad. Albeit the file that is created is valid and delimits well when imported into excel. However, I need the file this code generates to be readable when opened with a simple text editor. Any suggestions?

My CSV file handle looks like this:

CSVWriter wt = new CSVWriter(new FileWriter("file"), ',',
    CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, "\n");

回答1:


Use the System Properties: line separator instead of "\n" (on Windows, "\r\n" is end of line),

CSVWriter wt = new CSVWriter(new FileWriter("file"), ',', 
    CSVWriter.NO_QUOTE_CHARACTER, 
    CSVWriter.NO_ESCAPE_CHARACTER, 
    System.getProperty("line.separator"));

From the System Properties link above,

"line.seperator" Sequence used by operating system to separate lines in text files



来源:https://stackoverflow.com/questions/24400064/java-opencsv-writing-result-set-end-of-line-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!