Skip to content

Commit 9b37b1e

Browse files
committed
[CSV-192] Add convenience API CSVParser.parse(Path, Charset, CSVFormat)
1 parent 81f2400 commit 9b37b1e

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<action issue="CSV-189" type="add" dev="ggregory" due-to="Peter Holzwarth, Gary Gregory">CSVParser: Add factory method accepting InputStream.</action>
4444
<action issue="CSV-190" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(File, Charset)</action>
4545
<action issue="CSV-191" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(Path, Charset)</action>
46+
<action issue="CSV-192" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVParser.parse(Path, Charset, CSVFormat)</action>
4647
</release>
4748
<release version="1.4" date="2016-05-28" description="Feature and bug fix release">
4849
<action issue="CSV-181" type="update" dev="ggregory" due-to="Gary Gregory">Make CSVPrinter.print(Object) GC-free.</action>

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.UnsupportedEncodingException;
3131
import java.net.URL;
3232
import java.nio.charset.Charset;
33+
import java.nio.file.Path;
3334
import java.util.ArrayList;
3435
import java.util.Arrays;
3536
import java.util.Iterator;
@@ -192,6 +193,33 @@ public static CSVParser parse(final InputStream inputStream, final String charse
192193
return parse(new InputStreamReader(inputStream, charset), format);
193194
}
194195

196+
/**
197+
* Creates a parser for the given {@link File}.
198+
*
199+
* <p><strong>Note:</strong> This method internally creates a FileReader using
200+
* {@link java.io.FileReader#FileReader(java.io.File)} which in turn relies on the default encoding of the JVM that
201+
* is executing the code. If this is insufficient create a URL to the file and use
202+
* {@link #parse(URL, Charset, CSVFormat)}</p>
203+
*
204+
* @param path
205+
* a CSV file. Must not be null.
206+
* @param charset
207+
* A charset
208+
* @param format
209+
* the CSVFormat used for CSV parsing. Must not be null.
210+
* @return a new parser
211+
* @throws IllegalArgumentException
212+
* If the parameters of the format are inconsistent or if either file or format are null.
213+
* @throws IOException
214+
* If an I/O error occurs
215+
* @since 1.5
216+
*/
217+
public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException {
218+
Assertions.notNull(path, "path");
219+
Assertions.notNull(format, "format");
220+
return parse(path.toFile(), charset, format);
221+
}
222+
195223
/**
196224
* Creates a CSV parser using the given {@link CSVFormat}
197225
*

0 commit comments

Comments
 (0)