javacsv换⾏符_每⾏写⼊新的CSV⽂件(JAVA)我有以下代码:
public static void main(String[] args) throws IOException {
//File being read:
String fileName = "src/data/Belgium.csv";
String[] nextLine;
try (CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"', 1)) {
while ((nextLine = adNext()) != null) {
for (String line : nextLine) {
//NewFile
//When 2nd parameter - ture, it gets so big, that excel can't handle
FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", true);
line = placeAll("T", " ");
line = placeAll("Z", "");
line = placeAll("an", "");
line = placeAll("Plantname:", "");
//Escaping curly braces is a must!
line = placeAll("\\{", "");
line = placeAll("\\}", "");
writer.append(line);
writer.flush();
writer.close();
System.out.println(line);
}
}System.out.println("Successfully written");
}
}
在控制台中,使⽤System.out.println(line)输出的代码为我提供了正确的输出.
但是,当我打开CSV⽂件时,它似乎是反向写⼊的.
Excel⾸先抱怨⾏数.
但是,仅显⽰原始数据集的最后⼀⾏.
数据集(以⼀种低效的⽅式进⾏了预处理)包含1000多个⾏.因此,我不能简单地附加每个条⽬.
有更好的⽅法吗?
提⽰和技巧⾮常受欢迎.
更进⼀步,我尝试了⼏位作家:
-CSVwrite
-BufferedWriter
-FileWriter
还检查了关于Stackoverflow的其他问题…
似乎⽆法使其正常⼯作.谢谢!
更新:
问题得到解答!最终代码:
public static void main(String[] args) throws IOException {
//File being read:
String fileName = "src/data/Belgium.csv";
/
/When 2nd parameter - ture, it gets so big, that excel can't handle FileWriter writer = new FileWriter("src/dataNew/BelgiumNew5.csv", true);
String[] nextLine;
try (CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"', 1)) { while ((nextLine = adNext()) != null) {
for (String line : nextLine) {
line = placeAll("T", " ");
line = placeAll("Z", "");
line = placeAll("an", "");
line = placeAll("Plantname:", "");
//Escaping curly braces is a must!
line = placeAll("\\{", "");
line = placeAll("\\}", "");
writer.append(line);
writer.append(System.lineSeparator());
System.out.println(line);
}
}System.out.println("Successfully written");
}catch(Exception e){
e.printStackTrace();
}finally {
if (writer != null){
writer.flush();
}
}
}
replaceall()
解决⽅法:
However, when I open the CSV file, it seems like it is written
reversed. Excel first complains about the amount of rows. However,
only the last row of my original dataset shows.
我认为这可能是由于CSV⾏之间缺少换⾏符引起的.
实际上,当您在⽂件中写⼀⾏时,您不会写换⾏符.
您可以在每个读取⾏之后编写它:writer.append(System.lineSeparator()).
作为旁注:
1)为什么不在循环之前将其移动(否则效率较低):
FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", true);
2)您不应该在每个读取⾏刷新并关闭⽂件,因为它效率较低:
writer.append(line);
writer.flush();
writer.close();
System.out.println(line);
应该⾜够了:
writer.append(line);
System.out.println(line);
保持:
writer.flush();
writer.close();
在最终声明中,例如:
FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", true); try{
// all your operations to handle the file
}
catch(Exception e){
// your exception handling
}
finally{
writer.flush();
writer.close();
}
}
编辑以回答评论:
如果您觉得输出⽂件包含多个记录集,则它可能与您使⽤的FileWriter的附加模式有关.替换为:
FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", true);
以此不使⽤此模式:
FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", false);标签:java,csv,writer,filewriter

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。