org.apache.commons.csv.CSVParser.parse(File, Charset, CSVFormat) is now like org.apache.commons.csv.CSVParser.parse(URL, Charset, CSVFormat): You must pass in a Charset.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1602901 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2014-06-16 15:39:52 +00:00
parent 49579ec643
commit 229b3b7691
3 changed files with 13 additions and 10 deletions

View File

@ -17,10 +17,9 @@
package org.apache.commons.csv; package org.apache.commons.csv;
import static org.apache.commons.csv.Token.Type.TOKEN;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -37,6 +36,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import static org.apache.commons.csv.Token.Type.*;
/** /**
* Parses CSV files according to the specified format. * Parses CSV files according to the specified format.
* *
@ -50,7 +51,7 @@ import java.util.NoSuchElementException;
* There are several static factory methods that can be used to create instances for various types of resources: * There are several static factory methods that can be used to create instances for various types of resources:
* </p> * </p>
* <ul> * <ul>
* <li>{@link #parse(java.io.File, CSVFormat)}</li> * <li>{@link #parse(java.io.File, Charset, CSVFormat)}</li>
* <li>{@link #parse(String, CSVFormat)}</li> * <li>{@link #parse(String, CSVFormat)}</li>
* <li>{@link #parse(java.net.URL, java.nio.charset.Charset, CSVFormat)}</li> * <li>{@link #parse(java.net.URL, java.nio.charset.Charset, CSVFormat)}</li>
* </ul> * </ul>
@ -142,6 +143,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* *
* @param file * @param file
* a CSV file. Must not be null. * a CSV file. Must not be null.
* @param charset
* A charset
* @param format * @param format
* the CSVFormat used for CSV parsing. Must not be null. * the CSVFormat used for CSV parsing. Must not be null.
* @return a new parser * @return a new parser
@ -150,11 +153,11 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @throws IOException * @throws IOException
* If an I/O error occurs * If an I/O error occurs
*/ */
public static CSVParser parse(final File file, final CSVFormat format) throws IOException { public static CSVParser parse(final File file, Charset charset, final CSVFormat format) throws IOException {
Assertions.notNull(file, "file"); Assertions.notNull(file, "file");
Assertions.notNull(format, "format"); Assertions.notNull(format, "format");
// Use the default Charset explicitly
return new CSVParser(new FileReader(file), format); return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);
} }
/** /**

View File

@ -112,7 +112,7 @@ public class CSVFileParserTest {
// Now parse the file and compare against the expected results // Now parse the file and compare against the expected results
// We use a buffered reader internally so no need to create one here. // We use a buffered reader internally so no need to create one here.
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), format); final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format);
for(final CSVRecord record : parser) { for(final CSVRecord record : parser) {
String parsed = record.toString(); String parsed = record.toString();
if (checkComments) { if (checkComments) {

View File

@ -806,12 +806,12 @@ public class CSVParserTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testParseFileNullFormat() throws Exception { public void testParseFileNullFormat() throws Exception {
CSVParser.parse(new File(""), null); CSVParser.parse(new File(""), Charset.defaultCharset(), null);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testParseNullFileFormat() throws Exception { public void testParseNullFileFormat() throws Exception {
CSVParser.parse((File) null, CSVFormat.DEFAULT); CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -821,7 +821,7 @@ public class CSVParserTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testParseNullUrlCharsetFormat() throws Exception { public void testParseNullUrlCharsetFormat() throws Exception {
CSVParser.parse(null, Charset.defaultCharset(), CSVFormat.DEFAULT); CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)