Class CSVParser

原文地址:http://ostermiller.org/utils/javadoc/CSVParser.html

Each line is one entry or record and the fields in a record are separated by commas。每一行是一个整体或者是一条记录,被逗号分隔开

Commas may be preceded or followed by arbitrary space and/or tab characters which are ignored.逗号前后的空格是被忽视的

Commas may be preceded or followed by arbitrary space and/or tab characters which are ignored.如果一条记录不止一行,那么所有的行都是被引号括起来

When the field is in quotes, any quote literals must be escaped by \" Backslash literals must be escaped by \\. Otherwise a backslash and the character following will be treated as the following character, IE. "\n" is equivalent to "n". Other escape sequences may be set using the setEscapes() method.里面一些特殊字符是需要被转义的

Text that comes after quotes that have been closed but come before the next comma will be ignored.在引号结束,下一个引号开始之前,中间的内容都是被忽视的。

Empty fields are returned as as String of length zero: "".

Blank lines are always ignored. Other lines will be ignored if they start with a comment character as set by the setCommentStart() method.


An example of how CSVParser might be used:

 CSVParser shredder = new CSVParser(System.in);
 shredder.setCommentStart("#;!");
 shredder.setEscapes("nrtf", "\n\r\t\f");
 String t;
 while ((t = shredder.nextValue()) != null){
     System.out.println("" + shredder.lastLineNumber() + " " + t);
 }



构造函数:

CSVParser(InputStream in)
          Create a parser to parse comma separated values from an InputStream.
CSVParser(InputStream in, char delimiter)
          Create a parser to parse delimited values from an InputStream.
CSVParser(InputStream in, char delimiter, String escapes, String replacements, String commentDelims)
          Create a parser to parse delimited values from an InputStream.
CSVParser(InputStream in, String escapes, String replacements, String commentDelims)
          Create a parser to parse comma separated values from an InputStream.
CSVParser(Reader in)
          Create a parser to parse comma separated values from a Reader.
CSVParser(Reader in, char delimiter)
          Create a parser to parse delimited values from a Reader.
CSVParser(Reader in, char delimiter, String escapes, String replacements, String commentDelims)
          Create a parser to parse delimited values from a Reader.
CSVParser(Reader in, String escapes, String replacements, String commentDelims)
          Create a parser to parse comma separated values from a Reader.


主要方法:

 void changeDelimiter(char newDelim)
          Change this parser so that it uses a new delimiter.
 void changeQuote(char newQuote)
          Change this parser so that it uses a new character for quoting.
 void close()
          Close any stream upon which this parser is based.
 String[][] getAllValues()
          Get all the values from the file.
 int getLastLineNumber()
          Get the number of the line from which the last value was retrieved.
 String[] getLine()
          Get all the values from a line.
 int lastLineNumber()
          Get the line number that the last token came from.
 String nextValue()
          get the next value.
static String[][] parse(Reader in)
          Parse the delimited data from a stream.
static String[][] parse(Reader in, char delimiter)
          Parse the comma delimited data from a stream.
static String[][] parse(Reader in, char delimiter, String escapes, String replacements, String commentDelims)
          Parse the delimited data from a stream.
static String[][] parse(Reader in, String escapes, String replacements, String commentDelims)
          Parse the comma delimited data from a stream.
static String[][] parse(String s)
          Parse the comma delimited data from a string.
static String[][] parse(String s, char delimiter)
          Parse the delimited data from a string.
static String[][] parse(String s, char delimiter, String escapes, String replacements, String commentDelims)
          Parse the delimited data from a string.
static String[][] parse(String s, String escapes, String replacements, String commentDelims)
          Parse the comma delimited data from a string.
 void setCommentStart(String commentDelims)
          Set the characters that indicate a comment at the beginning of the line.
 void setEscapes(String escapes, String replacements)
          Specify escape sequences and their replacements.

你可能感兴趣的:(csv)