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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章