我正在从url读取csv文件,并将此文件复制到新的csv文件中。


这个csv文件是 -
分开的。我用,
替换 -
。
读取10 mb以上的大文件需要两倍的时间。我可以使用什么来将 -
分隔的文件转换为,
分隔的文件?
网址url = null;
尝试{
 url = new URL(info.getUrl());
} catch(MalformedURLException e1){
 e1.printStackTrace();
}
 Scanner scanner = null;
 StringBuilder sb = new StringBuilder();
 scanner = new Scanner(url.openStream());&# xA; while(scanner.hasNext()){
 String line = scanner.nextLine();
 sb.append(line.replace(“,”,“ - ”)。replace(“|”,“,”));
 sb.append( “\ n”);
}
 代码>


答案 0 :(得分:0)
使用univocity-parsers,您只需要:
//configure the input format
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setDelimiter('-');
//configure the ouput format
CsvWriterSettings writerSettings = new CsvWriterSettings();
writerSettings.getFormat().setDelimiter(',');
//use a class for common pre-made routines used for dealing with CSV
CsvRoutines csvRoutines = new CsvRoutines(parserSettings, writerSettings);
//use the `parseAndWrite` method to read the input in a given format and write it back to another.
csvRoutines.parseAndWrite(new FileReader("/path/to/input.csv"), new FileWriter("/path/to/output.csv"));
希望这有帮助
免责声明:我是这个图书馆的作者。它的开源和免费(Apache 2.0许可证)