java csv 换行符_每行写入新的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 = reader.readNext()) != null) {

for (String line : nextLine) {

//NewFile

//When 2nd parameter - ture, it gets so big, that excel can't handle it anymore...

FileWriter writer = new FileWriter("src/dataNew/BelgiumNew1.csv", true);

line = line.replaceAll("T", " ");

line = line.replaceAll("Z", "");

line = line.replaceAll("ActualGenerationPerUnit.mean", "");

line = line.replaceAll("Plantname:", "");

//Escaping curly braces is a must!

line = line.replaceAll("\\{", "");

line = line.replaceAll("\\}", "");

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 it anymore...

FileWriter writer = new FileWriter("src/dataNew/BelgiumNew5.csv", true);

String[] nextLine;

try (CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"', 1)) {

while ((nextLine = reader.readNext()) != null) {

for (String line : nextLine) {

line = line.replaceAll("T", " ");

line = line.replaceAll("Z", "");

line = line.replaceAll("ActualGenerationPerUnit.mean", "");

line = line.replaceAll("Plantname:", "");

//Escaping curly braces is a must!

line = line.replaceAll("\\{", "");

line = line.replaceAll("\\}", "");

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();

writer.close();

}

}

}

解决方法:

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{

if (writer!=null){

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

来源: https://codeday.me/bug/20191013/1905973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值