用于Java的CSV API
有谁能推荐一个简单的API,让我可以使用它读取CSV输入文件,进行一些简单的转换,然后编写它 谷歌很快发现了一个看起来很有希望的方法用于Java的CSV API,java,csv,Java,Csv,有谁能推荐一个简单的API,让我可以使用它读取CSV输入文件,进行一些简单的转换,然后编写它 谷歌很快发现了一个看起来很有希望的方法 在我将自己与这个API结合之前,我只是想检查一下其他人在使用什么。我们使用的,它工作得很好,它工作得很好CSV格式对于StringTokenizer来说听起来很简单,但它可能会变得更复杂。 在德国,分号用作分隔符,包含分隔符的单元格需要转义。使用StringTokenizer,您将无法轻松处理这一问题 对于StringTokenizer,CSV格式听起来很简单,但
在我将自己与这个API结合之前,我只是想检查一下其他人在使用什么。我们使用的,它工作得很好,它工作得很好CSV格式对于StringTokenizer来说听起来很简单,但它可能会变得更复杂。 在德国,分号用作分隔符,包含分隔符的单元格需要转义。使用StringTokenizer,您将无法轻松处理这一问题
对于StringTokenizer,CSV格式听起来很简单,但它可能会变得更复杂。 在德国,分号用作分隔符,包含分隔符的单元格需要转义。使用StringTokenizer,您将无法轻松处理这一问题
我会选择作为我开发的最后一个企业应用程序,它需要处理大量CSV——几个月前——我在sourceforge使用过,发现它很简单,健壮且无问题。对于我开发的上一个需要处理大量CSV的企业应用程序——几个月前——我在sourceforge使用过,发现它简单、健壮且无问题。如果您打算从excel中读取CSV,那么有一些有趣的例子。我记不清它们的全部内容,但ApacheCommonsCSV无法正确处理它们(例如,使用URL)
请务必使用引号、逗号和斜杠测试excel输出。如果您打算从excel读取csv,则有一些有趣的例子。我记不清它们的全部内容,但ApacheCommonsCSV无法正确处理它们(例如,使用URL) 一定要用引号、逗号和斜杠测试excel输出。Apache Commons CSV 退房 此库读取和写入,包括标准库。还可以读取/写入文件
- 胜过
- InformixUnload
- InformixUnloadCsv
- MySQL
- 神谕
- PostgreSQLCsv
- PostgreSQLText
- RFC4180
- TDF
- 胜过
- InformixUnload
- InformixUnloadCsv
- MySQL
- 神谕
- PostgreSQLCsv
- PostgreSQLText
- RFC4180
- TDF
import au.com.bytecode.opencsv.CSVReader;
字符串fileName=“data.csv”;
CSVReader reader=new CSVReader(new FileReader(fileName))
//如果第一行是标题
String[]header=reader.readNext()//迭代reader.readNext,直到它返回null String[]line=reader.readNext(); 答案中还有其他一些选择 我过去用过
import au.com.bytecode.opencsv.CSVReader;
字符串fileName=“data.csv”;
CSVReader reader=new CSVReader(new FileReader(fileName))
//如果第一行是标题
String[]header=reader.readNext()//迭代reader.readNext,直到它返回null String[]line=reader.readNext();
答案中还有其他一些选择 更新:此答案中的代码适用于超级CSV 1.52。Super CSV 2.4.0的更新代码示例可在项目网站上找到:
SuperCSV项目直接支持CSV单元格的解析和结构化操作。从你会发现 上课
public class UserBean {
String username, password, street, town;
int zip;
public String getPassword() { return password; }
public String getStreet() { return street; }
public String getTown() { return town; }
public String getUsername() { return username; }
public int getZip() { return zip; }
public void setPassword(String password) { this.password = password; }
public void setStreet(String street) { this.street = street; }
public void setTown(String town) { this.town = town; }
public void setUsername(String username) { this.username = username; }
public void setZip(int zip) { this.zip = zip; }
}
并且您有一个带有头的CSV文件。让我们假设以下内容
username, password, date, zip, town
Klaus, qwexyKiks, 17/1/2007, 1111, New York
Oufu, bobilop, 10/10/2007, 4555, New York
然后,您可以创建UserBean的一个实例,并用以下代码填充文件第二行中的值
class ReadingObjects {
public static void main(String[] args) throws Exception{
ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
try {
final String[] header = inFile.getCSVHeader(true);
UserBean user;
while( (user = inFile.read(UserBean.class, header, processors)) != null) {
System.out.println(user.getZip());
}
} finally {
inFile.close();
}
}
}
使用以下“操作规范”
更新:此答案中的代码适用于超级CSV 1.52。Super CSV 2.4.0的更新代码示例可在项目网站上找到:
SuperCSV项目直接支持CSV单元格的解析和结构化操作。从你会发现 上课
public class UserBean {
String username, password, street, town;
int zip;
public String getPassword() { return password; }
public String getStreet() { return street; }
public String getTown() { return town; }
public String getUsername() { return username; }
public int getZip() { return zip; }
public void setPassword(String password) { this.password = password; }
public void setStreet(String street) { this.street = street; }
public void setTown(String town) { this.town = town; }
public void setUsername(String username) { this.username = username; }
public void setZip(int zip) { this.zip = zip; }
}
并且您有一个带有头的CSV文件。让我们假设以下内容
username, password, date, zip, town
Klaus, qwexyKiks, 17/1/2007, 1111, New York
Oufu, bobilop, 10/10/2007, 4555, New York
然后,您可以创建UserBean的一个实例,并用以下代码填充文件第二行中的值
class ReadingObjects {
public static void main(String[] args) throws Exception{
ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
try {
final String[] header = inFile.getCSVHeader(true);
UserBean user;
while( (user = inFile.read(UserBean.class, header, processors)) != null) {
System.out.println(user.getZip());
}
} finally {
inFile.close();
}
}
}
使用以下“操作规范”
还有。它假定所有这些数据都是类似于表的,并从迭代器传递数据。也有。它假设所有这些数据都是类似于表的,并从迭代器中传递数据。您可以使用csvreader api并从以下位置下载: 或 使用以下代码:
/ ************ For Reading ***************/
import java.io.FileNotFoundException;
import java.io.IOException;
import com.csvreader.CsvReader;
public class CsvReaderExample {
public static void main(String[] args) {
try {
CsvReader products = new CsvReader("products.csv");
products.readHeaders();
while (products.readRecord())
{
String productID = products.get("ProductID");
String productName = products.get("ProductName");
String supplierID = products.get("SupplierID");
String categoryID = products.get("CategoryID");
String quantityPerUnit = products.get("QuantityPerUnit");
String unitPrice = products.get("UnitPrice");
String unitsInStock = products.get("UnitsInStock");
String unitsOnOrder = products.get("UnitsOnOrder");
String reorderLevel = products.get("ReorderLevel");
String discontinued = products.get("Discontinued");
// perform program logic here
System.out.println(productID + ":" + productName);
}
products.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入/附加到CSV文件
代码:
您可以使用csvreader api并从以下位置下载: 或 使用以下代码:
/ ************ For Reading ***************/
import java.io.FileNotFoundException;
import java.io.IOException;
import com.csvreader.CsvReader;
public class CsvReaderExample {
public static void main(String[] args) {
try {
CsvReader products = new CsvReader("products.csv");
products.readHeaders();
while (products.readRecord())
{
String productID = products.get("ProductID");
String productName = products.get("ProductName");
String supplierID = products.get("SupplierID");
String categoryID = products.get("CategoryID");
String quantityPerUnit = products.get("QuantityPerUnit");
String unitPrice = products.get("UnitPrice");
String unitsInStock = products.get("UnitsInStock");
String unitsOnOrder = products.get("UnitsOnOrder");
String reorderLevel = products.get("ReorderLevel");
String discontinued = products.get("Discontinued");
// perform program logic here
System.out.println(productID + ":" + productName);
}
products.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入/附加到CSV文件
代码:
阅读CSV格式的描述让我觉得使用第三方库比自己编写更省心:
+ maven
+ maven - release version // had some cryptic issues at _Hudson_ with snapshot references => prefer to be on a safe side
+ code examples
+ open source // as in "can hack myself if needed"
+ understandable javadoc // as opposed to eg javadocs of _genjava gj-csv_
+ compact API // YAGNI (note *flatpack* seems to have much richer API than OpenCSV)
- reference to specification used // I really like it when people can explain what they're doing
- reference to _RFC 4180_ support // would qualify as simplest form of specification to me
- releases changelog // absence is quite a pity, given how simple it'd be to get with maven-changes-plugin // _flatpack_, for comparison, has quite helpful changelog
+ bug tracking
+ active // as in "can submit a bug and expect a fixed release soon"
+ positive feedback // Recommended By 51 users at sourceforge (as of now)
阅读CSV格式的描述让我觉得使用第三方库比自己编写更省心:
+ maven
+ maven - release version // had some cryptic issues at _Hudson_ with snapshot references => prefer to be on a safe side
+ code examples
+ open source // as in "can hack myself if needed"
+ understandable javadoc // as opposed to eg javadocs of _genjava gj-csv_
+ compact API // YAGNI (note *flatpack* seems to have much richer API than OpenCSV)
- reference to specification used // I really like it when people can explain what they're doing
- reference to _RFC 4180_ support // would qualify as simplest form of specification to me
- releases changelog // absence is quite a pity, given how simple it'd be to get with maven-changes-plugin // _flatpack_, for comparison, has quite helpful changelog
+ bug tracking
+ active // as in "can submit a bug and expect a fixed release soon"
+ positive feedback // Recommended By 51 users at sourceforge (as of now)
+1,但它有一些严重的错误尚未修复,新的错误目前还没有处理,最近的版本已经将近两年了。但我们在生产中使用的是经过修补/修改的版本,没有任何问题。@MRalwasser最近在