public class CSVUtil extends Object
RFC 4180 standard Reliance on the standard documented by RFC 4180 can simplify CSV exchange. However, this standard only specifies handling of text-based fields. Interpretation of the text of each field is still application-specific.
RFC 4180 formalized CSV. It defines the MIME type "text/csv", and CSV files that follow its rules should be very widely portable. Among its requirements:
1. MS-DOS-style lines that end with (CR/LF) characters (optional for the last line). 2. An optional header record (there is no sure way to detect whether it is present, so care is required when importing). 3. Each record "should" contain the same number of comma-separated fields. 4. Any field may be quoted (with double quotes). 5. Fields containing a line-break, double-quote or commas should be quoted. (If they are not, the file will likely be impossible to process correctly). 6. A (double) quote character in a field must be represented by two (double) quote characters.
Create by guanquan.wang at 2019-02-12 17:27
| 限定符和类型 | 类和说明 |
|---|---|
static class |
CSVUtil.Reader
A CSV format file reader.
|
static class |
CSVUtil.RowsIterator
Rows iterator
|
static class |
CSVUtil.SharedRowsIterator
Shared Row iterator
|
static class |
CSVUtil.Writer
A CSV format file writer.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static CSVUtil.Reader |
newReader(Path path)
Create a CSV reader.
|
static CSVUtil.Reader |
newReader(Path path,
char separator)
Create a CSV reader.
|
static CSVUtil.Reader |
newReader(Path path,
char separator,
Charset charset)
Create a CSV reader.
|
static CSVUtil.Reader |
newReader(Path path,
Charset charset)
Create a CSV reader.
|
static CSVUtil.Writer |
newWriter(BufferedWriter writer)
Create a CSV writer
|
static CSVUtil.Writer |
newWriter(OutputStream os)
Create a CSV writer
|
static CSVUtil.Writer |
newWriter(Path path)
Create a CSV writer
|
static CSVUtil.Writer |
newWriter(Path path,
char separator)
Create a CSV writer
|
static CSVUtil.Writer |
newWriter(Path path,
char separator,
Charset charset)
Create a CSV writer
|
static CSVUtil.Writer |
newWriter(Path path,
Charset charset)
Create a CSV writer
|
static List<String[]> |
read(Path path)
Read csv format file by UTF-8 charset.
|
static List<String[]> |
read(Path path,
char separator)
Read csv format file by UTF-8 charset.
|
static List<String[]> |
read(Path path,
char separator,
Charset charset)
Read csv format file.
|
static List<String[]> |
read(Path path,
Charset charset)
Read csv format file.
|
static <T> List<T> |
read(Path path,
Class<T> clazz)
Read csv format file by UTF-8 charset.
|
static <T> List<T> |
read(Path path,
Class<T> clazz,
Charset charset)
Read csv format file.
|
static void |
writeTo(List<?> data,
Path path)
Save vector object as csv format file
|
public static <T> List<T> read(Path path, Class<T> clazz) throws IOException
T - the result typepath - the csv file pathclazz - the class convert toIOException - if I/O error occurpublic static List<String[]> read(Path path) throws IOException
path - the csv file pathIOException - if I/O error occurpublic static List<String[]> read(Path path, char separator) throws IOException
path - the csv file pathseparator - the separator characterIOException - if I/O error occurpublic static <T> List<T> read(Path path, Class<T> clazz, Charset charset) throws IOException
T - the result typepath - the csv file pathclazz - the class convert tocharset - the charset to use for encodingIOException - if I/O error occurpublic static List<String[]> read(Path path, Charset charset) throws IOException
path - the csv file pathcharset - the charset to use for encodingIOException - if I/O error occurpublic static List<String[]> read(Path path, char separator, Charset charset) throws IOException
path - the csv file pathseparator - the separator charactercharset - the charset to use for encodingIOException - if I/O error occurpublic static CSVUtil.Reader newReader(Path path)
path - the csv file pathpublic static CSVUtil.Reader newReader(Path path, Charset charset)
path - the csv file pathcharset - the charset to use for encodingpublic static CSVUtil.Reader newReader(Path path, char separator)
path - the csv file pathseparator - the separator characterpublic static CSVUtil.Reader newReader(Path path, char separator, Charset charset)
path - the csv file pathseparator - the separator charactercharset - the charset to use for encodingpublic static void writeTo(List<?> data, Path path) throws IOException
data - the vector object to be savepath - the save pathIOException - if I/O error occurpublic static CSVUtil.Writer newWriter(Path path) throws IOException
path - the storage pathIOException - no permission or other I/O error occurpublic static CSVUtil.Writer newWriter(Path path, Charset charset) throws IOException
path - the storage pathcharset - the charset to use for encodingIOException - no permission or other I/O error occurpublic static CSVUtil.Writer newWriter(Path path, char separator) throws IOException
path - the storage pathseparator - the separator characterIOException - no permission or other I/O error occurpublic static CSVUtil.Writer newWriter(Path path, char separator, Charset charset) throws IOException
path - the storage pathseparator - the separator charactercharset - the charset to use for encodingIOException - no permission or other I/O error occurpublic static CSVUtil.Writer newWriter(BufferedWriter writer)
writer - the BufferedWriterpublic static CSVUtil.Writer newWriter(OutputStream os)
os - the OutputStreamCopyright © 2019. All rights reserved.