mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-07 18:49:24 +00:00
Added a convenient parse() method to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297022 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c7f90f93c6
commit
312f5b033e
@ -17,6 +17,7 @@
|
||||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@ -198,6 +199,15 @@ public class CSVFormat implements Cloneable, Serializable {
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the specified content.
|
||||
*
|
||||
* @param in
|
||||
*/
|
||||
public Iterable<String[]> parse(Reader in) {
|
||||
return new CSVParser(in, this);
|
||||
}
|
||||
|
||||
protected CSVFormat clone() {
|
||||
try {
|
||||
return (CSVFormat) super.clone();
|
||||
|
@ -573,22 +573,22 @@ public class CSVParserTest extends TestCase {
|
||||
public void testForEach() {
|
||||
List<String[]> records = new ArrayList<String[]>();
|
||||
|
||||
String code = "a,b,c\n1,2,3\nx,y,z";
|
||||
Reader in = new StringReader(code);
|
||||
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
for (String[] record : new CSVParser(in)) {
|
||||
for (String[] record : CSVFormat.DEFAULT.parse(in)) {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
assertEquals(3, records.size());
|
||||
assertTrue(Arrays.equals(new String[] {"a", "b", "c"}, records.get(0)));
|
||||
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
|
||||
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
|
||||
assertTrue(Arrays.equals(new String[] {"x", "y", "z"}, records.get(2)));
|
||||
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
|
||||
}
|
||||
|
||||
public void testIterator() {
|
||||
String code = "a,b,c\n1,2,3\nx,y,z";
|
||||
Iterator<String[]> iterator = new CSVParser(new StringReader(code)).iterator();
|
||||
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
iterator.remove();
|
||||
|
Loading…
x
Reference in New Issue
Block a user