在java中写入csv文件 [英] Writing to a csv file in java

查看:205
本文介绍了在java中写入csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 java 程序来将数据写入一个 csv 文件,该文件从数据库中获取键的计数值并写入文件中每个键对应的计数.我已经使用 FileWriter 完成了它,其伪代码如下

I am writing a java program to write data to a csv file which fetches a key's count value from database and writes the count corresponding to each key in the file. I have accomplished it using FileWriter whose pseudocode goes like below

while (keys.hasNext()) {
    writer.append(keys.next().getCount());
    writer.append(',');
}

// where keys is the list of the keys

标题也以上述方式附加.现在我遇到了 OpenCSV 和 CommonsCSV 等用于写入 csv 文件的开源库.

The headers are also appended in the above way. Now I have come across open source libraries such as OpenCSV and CommonsCSV for writing to csv files.

所以现在想知道是使用库更好还是使用上述写入 CSV 文件的方式更好.有人能告诉我哪种方式在可读性和效率方面更好吗?

So now am wondering whether using the libraries is better or using the above mentioned way of writing to CSV file. Can someone please tell me which way is better in terms of readability and efficiency?

推荐答案

这完全取决于您.这是与您的代码等效的 OpenCSV:

It's pretty much up to you. Here's the OpenCSV equivalent of your code:

 CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
 ...
 String[] row = new String[];
 int i=0;
 while(keys.hasNext()) {
     row[i++] = keys.next().getCount();
 }
 writer.writeNext(entries);

它的可读性比你的高还是低?这是主观的,由您决定.我可以告诉你,你的效率并不低.

Is that more or less readable than yours? That's subjective and up to you. I can tell you that yours is not inefficient.

值得注意的是,您的代码将在每行末尾写上,".图书馆不会.您的代码可以这样更改:

It's worth noting that your code will write "," at the end of each line. The library will not. Your code could be changed like this:

boolean more = keys.hasNext();
while (more) {
   writer.append(keys.next().getCount());
   more = keys.hasNext();
   if(more) {
      writer.append(',');
   }
}

CSV 看起来很简单,而且通常很简单,直到您开始遇到更复杂的情况,例如包含逗号或转义引号的引用字段:

CSV seems simple, and usually is, until you start encountering more complex situations, such as quoted fields containing commas or escaped quotes:

 A field,"another field","a field, containing a comma","A \"field\""

如果您的程序遇到这样的情况,它就会崩溃,您需要增强您的 CSV 算法来处理它.如果您使用的是一个库,您可能会有一些合理的期望,即它会从一开始就处理引号和带引号的逗号.您认为这种情况的可能性有多大取决于您.

If your program encounters a situation like this, it will break, and you'd need to enhance your CSV algorithms to handle it. If you were using a library, you could have some reasonable expectation that it would handle quotes and quoted commas from the outset. It's up to you how likely you think that situation is.

编写 CSV 代码通常很简单,但也有陷阱,需要维护的代码越少越好.

Writing CSV code is usually simple, but there are pitfalls and it's always good to have less code to maintain.

使用库有其自身的开销——管理依赖项等.

Using a library has its own overheads -- managing the dependencies and so on.

对于您现在正在做的简单事情,您可能不需要库.如果您自己的代码变得更加复杂,或者您开始​​需要将 bean 导出为 CSV 或处理包含引号逗号的 CSV 等功能,您可能会考虑使用一种.

You probably don't need a library for the simple stuff you're doing now. You might consider using one if your own code evolves to get more complicated, or if you start to need features like exporting beans to CSV, or handling CSV containing quoted commas.

这篇关于在java中写入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆