- Arbitrary numbers of values per line
- Ignoring commas in quoted elements
- Handling quoted entries with embedded carriage returns (ie entries that span multiple lines)
- Configurable separator and quote characters (or use sensible defaults)
- Read all the entries at once, or use an Iterator style model
- Creating csv files from String[] (ie. automatic escaping of embedded quote chars)
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
List, just call
readAll()...
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
List
of
String[]
that you can iterate over. If all else fails, check out the
Javadoc.
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 2);
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
// feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
writer.close();
writeAll()
a ResultSet.
java.sql.ResultSet myResultSet = ....
writer.writeAll(myResultSet, includeHeaders);
au.com.bytecode.opencsv.bean
package. Here's how you can map to a java bean based on the field positions in your CSV file:
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(YourOrderBean.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, yourReader);
645




被折叠的 条评论
为什么被折叠?



