在SuperCSV中解析字符串 [英] Parsing Strings in SuperCSV
问题描述
@Carlo V. Dango
我已经简化了我的问题,我已经阅读文档 - 好建议不要恐慌。仍然,我有问题。帮助我解决一个,它会解决所有的。谢谢。
问题:当我有一个缺少非字符串字段的csv记录时,如何(甚至可以)将缺少的条目转换为默认值,或者至少,不抛出NullPointerException?可选的cellProcessor看起来不会阻止错误。
这个程序基本上来自SuperCSV网站。
package com.test.csv;
import java.io.FileReader;
import org.supercsv.cellprocessor.ParseBigDecimal;
import org.supercsv.cellprocessor.ParseDate;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
public class CSVReader {
private static final CellProcessor [] cellProcessor = new CellProcessor [] {
null,
null,
new ParseInt(),
new ParseDate(yyyyMMdd),
new ParseBigDecimal()
};
public static void main(String [] args)throws Exception {
CsvPreference pref = new CsvPreference('','|',\\\
);
ICsvBeanReader inFile = new CsvBeanReader(new FileReader(C:\\temp\\\sapfilePipe.txt),pref);
try {
final String [] header = inFile.getCSVHeader(true);
用户;
while((user = inFile.read(User.class,header,cellProcessor))!= null){
System .out.println(user);
}
} finally {
inFile.close();
}
}
}
这里是我正在阅读的CSV文件。在第一个记录中有一个缺少字段(age)。
firstName | lastName | age | hireDate | hourlyRate
A. | Smith | | 20110101 | 15.50
我的用户bean:
package com.test.csv;
import java.math.BigDecimal;
import java.util.Date;
public class User {
private String firstName;
private String lastName;
private int age;
private日期hireDate;
private BigDecimal hourlyRate;
... getters / setters ...
这是错误:
线程main中的异常java.lang.NullPointerException
at org.supercsv.io.CsvBeanReader.fillObject(Unknown Source)
at org.supercsv.io.CsvBeanReader.read(未知源)
at com.glazers.csv.CSVReader.main(CSVReader.java:31)
感谢。
解决方案行到字符串列表。这似乎是你要找的。 p>
http://supercsv.sourceforge.net/javadoc/org/supercsv/io/CsvListReader.html
或此处显示 http://supercsv.sourceforge.net/codeExamples_general.html 您可以将处理器设置为null如果你不想要任何具体做。
@Carlo V. Dango
I have simplified my question and I have read the documentation--good advice not to panic. Still, I have problems. Help me solve one and it will solve them all. Thank you.
Question: When I have a csv record that is missing a non-String field, how (or even can I) convert the missing entry to a default value, or at least, not throw NullPointerException? Optional cellProcessor doesn't appear to prevent the error either.
This the program taken essentially from the SuperCSV website.
package com.test.csv;
import java.io.FileReader;
import org.supercsv.cellprocessor.ParseBigDecimal;
import org.supercsv.cellprocessor.ParseDate;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
public class CSVReader {
private static final CellProcessor[] cellProcessor = new CellProcessor[] {
null,
null,
new ParseInt(),
new ParseDate("yyyyMMdd"),
new ParseBigDecimal()
};
public static void main (String[] args ) throws Exception {
CsvPreference pref = new CsvPreference('"', '|', "\n");
ICsvBeanReader inFile = new CsvBeanReader(new FileReader("C:\\temp\\sapfilePipe.txt"), pref);
try {
final String[] header = inFile.getCSVHeader(true);
User user;
while ((user = inFile.read(User.class, header, cellProcessor)) != null) {
System.out.println(user);
}
} finally {
inFile.close();
}
}
}
here is the CSV file I'm reading. Notice in the first record there is a missing field (age).
firstName|lastName|age|hireDate|hourlyRate
A.|Smith| |20110101|15.50
My User bean:
package com.test.csv;
import java.math.BigDecimal;
import java.util.Date;
public class User {
private String firstName;
private String lastName;
private int age;
private Date hireDate;
private BigDecimal hourlyRate;
...getters/setters...
Here is the error:
Exception in thread "main" java.lang.NullPointerException
at org.supercsv.io.CsvBeanReader.fillObject(Unknown Source)
at org.supercsv.io.CsvBeanReader.read(Unknown Source)
at com.glazers.csv.CSVReader.main(CSVReader.java:31)
Thanks.
解决方案 The list reader reads each line into a list of strings. It seems this is what you are looking for.
http://supercsv.sourceforge.net/javadoc/org/supercsv/io/CsvListReader.html
or as shown here http://supercsv.sourceforge.net/codeExamples_general.html you can set the processor to null if you don't want anything specific done.
这篇关于在SuperCSV中解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!