我试图将我的mysql查询的内容转储到csv中,并使用一些基于java的开源csv阅读器阅读它。以下是我面临的问题,
如果我可以跳过换行符,但仍保留转义双引号以使csv读取器不中断,那将是很好的。
有什么建议可以在这里找到吗?
谢谢, 斯利拉姆
答案 0 :(得分:1)
尝试使用uniVocity-parsers将数据转储为CSV格式。然后,您可以使用相同的库读取结果:
尝试将数据转储出来:
ResultSet resultSet = executeYourQuery();
// To dump the data of our ResultSet, we configure the output format:
CsvWriterSettings writerSettings = new CsvWriterSettings();
writerSettings.getFormat().setLineSeparator("\n");
writerSettings.setHeaderWritingEnabled(true); // if you want want the column names to be printed out.
// Then create a routines object:
CsvRoutines routines = new CsvRoutines(writerSettings);
// The write() method takes care of everything. Both resultSet and output are closed by the routine.
routines.write(resultSet, new File("/path/to/your.csv"), "UTF-8");
这是为了阅读你的文件:
// creates a CSV parser
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\n");
parserSettings.setHeaderExtractionEnabled(true); //extract headers from file
CsvParser parser = new CsvParser(parserSettings);
// call beginParsing to read records one by one, iterator-style. Note that there are many ways to read your file, check the documentation.
parser.beginParsing(new File("/path/to/your.csv"), "UTF-8);
String[] row;
while ((row = parser.parseNext()) != null) {
System.out.println(Arrays.toString(row));
}
希望这有帮助。
免责声明:我是这个图书馆的作者,它的开源和免费(Apache V2.0许可证)