Class CSVParser

来源:互联网 发布:英文小说 知乎 编辑:程序博客网 时间:2024/06/04 20:12

原文地址: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.


主要方法:

 voidchangeDelimiter(char newDelim)
          Change this parser so that it uses a new delimiter. voidchangeQuote(char newQuote)
          Change this parser so that it uses a new character for quoting. voidclose()
          Close any stream upon which this parser is based. String[][]getAllValues()
          Get all the values from the file. intgetLastLineNumber()
          Get the number of the line from which the last value was retrieved. String[]getLine()
          Get all the values from a line. intlastLineNumber()
          Get the line number that the last token came from. StringnextValue()
          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. voidsetCommentStart(String commentDelims)
          Set the characters that indicate a comment at the beginning of the line. voidsetEscapes(String escapes,String replacements)
          Specify escape sequences and their replacements.

0 0