我一直在研究一个项目,从多个 .txt 文件中读取数据以将其转换为 .csv 文件。我的问题是导出的数据不断覆盖第一行,所以每次我导出文件时,它只显示最后一行。有没有人有任何提示可以帮助我?
//Reading the information from the files.
for (String fileName : textFileNames) {
try (Scanner sc = new Scanner(new File(stack + "\\" + fileName));) {
fileIn = sc.nextLine();
while (fileIn != null) {
String line = fileIn;
String[] split = line.split("\\s+");
StringJoiner joiner = new StringJoiner(",");
for (String strVal : split) {
joiner.add(strVal);
}
line = joiner.toString();
line = line.startsWith(",") ? line.substring(1) : line;
System.out.println(line);
BufferedWriter br = new BufferedWriter(new FileWriter("myfile.csv"));
StringBuilder sb = new StringBuilder();
// Append strings from array
sb.append(line);
sb.append("\n");
br.write(sb.toString());
br.close();
fileIn = sc.nextLine();
}
sc.close();
} catch (IOException ex) {
Logger.getLogger(Class_Organizer_Krause.class.getName())
.log(Level.SEVERE, null, ex);
}
}
开满天机
相关分类