openCSV 开源程序解析CSV文件

openCSV 开源程序解析CSV文件
2011年09月01日
   读CSV
  package au.com.bytecode.opencsv; /** Copyright 2005 Bytecode Pty Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; /** * A very simple CSV reader released under a commercial-friendly license. * * @author Glen Smith * */ public class CSVReader { private BufferedReader br; private boolean hasNext = true; private char separator; private char quotechar; private int skipLines; private boolean linesSkiped; /** The default separator to use if none is supplied to the constructor. */ public static final char DEFAULT_SEPARATOR = ','; /** * The default quote character to use if none is supplied to the * constructor. */ public static final char DEFAULT_QUOTE_CHARACTER = '"'; /** * The default line to start reading. */ public static final int DEFAULT_SKIP_LINES = 0; /** * Constructs CSVReader using a comma for the separator. * * @param reader * the reader to an underlying CSV source. */ public CSVReader(Reader reader) { this(reader, DEFAULT_SEPARATOR); } /** * Constructs CSVReader with supplied separator. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries. */ public CSVReader(Reader reader, char separator) { this(reader, separator, DEFAULT_QUOTE_CHARACTER); } /** * Constructs CSVReader with supplied separator and quote char. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements */ public CSVReader(Reader reader, char separator, char quotechar) { this(reader, separator, quotechar, DEFAULT_SKIP_LINES); } /** * Constructs CSVReader with supplied separator and quote char. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements * @param line * the line number to skip for start reading */ public CSVReader(Reader reader, char separator, char quotechar, int line) { this.br = new BufferedReader(reader); this.separator = separator; this.quotechar = quotechar; this.skipLines = line; } /** * Reads the entire file into a List with each element being a String[] of * tokens. * * @return a List of String[], with each String[] representing a line of the * file. * * @throws IOException * if bad things happen during the read */ public List readAll() throws IOException { List allElements = new ArrayList(); while (hasNext) { String[] nextLineAsTokens = readNext(); if (nextLineAsTokens != null) allElements.add(nextLineAsTokens); } return allElements; } /** * Reads the next line from the buffer and converts to a string array. * * @return a string array with each comma-separated element as a separate * entry. * * @throws IOException * if bad things happen during the read */ public String[] readNext() throws IOException { String nextLine = getNextLine(); return hasNext ? parseLine(nextLine) : null; } /** * Reads the next line from the file. * * @return the next line from the file without trailing newline * @throws IOException * if bad things happen during the read */ private String getNextLine() throws IOException { if (!this.linesSkiped) { for (int i = 0; i (i+1) // there is indeed another character to check. && nextLine.charAt(i+1) == quotechar ){ // ..and that char. is a quote also. // we have two quote chars in a row == one quote char, so consume them both and // put one on the token. we do *not* exit the quoted text. sb.append(nextLine.charAt(i+1)); i++; }else{ inQuotes = !inQuotes; // the tricky case of an embedded quote in the middle: a,bc"d"ef,g if(i>2 //not on the begining of the line && nextLine.charAt(i-1) != this.separator //not at the begining of an escape sequence && nextLine.length()>(i+1) && nextLine.charAt(i+1) != this.separator //not at the end of an escape sequence ){ sb.append(c); } } } else if (c == separator && !inQuotes) { tokensOnThisLine.add(sb.toString()); sb = new StringBuffer(); // start work on next token } else { sb.append(c); } } } while (inQuotes); tokensOnThisLine.add(sb.toString()); return (String[]) tokensOnThisLine.toArray(new String[0]); } /** * Closes the underlying reader. * * @throws IOException if the close fails */ public void close() throws IOException{ br.close(); } }
  写CSV
  package au.com.bytecode.opencsv; /** Copyright 2005 Bytecode Pty Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.sql.Clob; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; import java.util.Iterator; import java.util.List; /** * A very simple CSV writer released under a commercial-friendly license. * * @author Glen Smith * */ public class CSVWriter { private Writer rawWriter; private PrintWriter pw; private char separator; private char quotechar; private String lineEnd; /** The character used for escaping quotes. */ public static final char ESCAPE_CHARACTER = '"'; /** The default separator to use if none is supplied to the constructor. */ public static final char DEFAULT_SEPARATOR = ','; /** * The default quote character to use if none is supplied to the * constructor. */ public static final char DEFAULT_QUOTE_CHARACTER = '"'; /** The quote constant to use when you wish to suppress all quoting. */ public static final char NO_QUOTE_CHARACTER = '\u0000'; /** Default line terminator uses platform encoding. */ public static final String DEFAULT_LINE_END = "\n"; /** * Constructs CSVWriter using a comma for the separator. * * @param writer * the writer to an underlying CSV source. */ public CSVWriter(Writer writer) { this(writer, DEFAULT_SEPARATOR); } /** * Constructs CSVWriter with supplied separator. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries. */ public CSVWriter(Writer writer, char separator) { this(writer, separator, DEFAULT_QUOTE_CHARACTER); } /** * Constructs CSVWriter with supplied separator and quote char. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements */ public CSVWriter(Writer writer, char separator, char quotechar) { this(writer, separator, quotechar, "\n"); } /** * Constructs CSVWriter with supplied separator and quote char. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements * @param lineEnd * the line feed terminator to use */ public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd) { this.rawWriter = writer; this.pw = new PrintWriter(writer); this.separator = separator; this.quotechar = quotechar; this.lineEnd = lineEnd; } /** * Writes the entire list to a CSV file. The list is assumed to be a * String[] * * @param allLines * a List of String[], with each String[] representing a line of * the file. */ public void writeAll(List allLines) { for (Iterator iter = allLines.iterator(); iter.hasNext();) { String[] nextLine = (String[]) iter.next(); writeNext(nextLine); } } protected void writeColumnNames(ResultSetMetaData metadata) throws SQLException { int columnCount = metadata.getColumnCount(); String[] nextLine = new String[columnCount]; for (int i = 0; i 0) { sb.append(cbuf, 0, n); } } return sb.toString(); } /** * Writes the next line to the file. * * @param nextLine * a string array with each comma-separated element as a separate * entry. */ public void writeNext(String[] nextLine) { StringBuffer sb = new StringBuffer(); for (int i = 0; i
  
  
  
  
  
  
  
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值