⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csvreader.java

📁 一种解析csv文件特别好的方法
💻 JAVA
字号:
/*
 *  CsvJdbc - a JDBC driver for CSV files
 *  Copyright (C) 2001  Jonathan Ackerman
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.relique.jdbc.csv;

import java.io.*;
import java.util.*;
import java.sql.SQLException;

/**
 * This class is a helper class that handles the reading and parsing of data
 * from a .csv file.
 *
 * @author     Jonathan Ackerman
 * @author     Sander Brienen
 * @author     Stuart Mottram (fritto)
 * @author     Jason Bedell
 * @author     Tomasz Skutnik
 * @created    25 November 2001
 * @version    $Id: CsvReader.java,v 1.11 2004/04/09 11:17:13 gupta_chetan Exp $
 */

public class CsvReader extends CSVReaderAdapter
{
  private BufferedReader input;

  /**
   *Constructor for the CsvReader object
   *
   * @param  fileName       Description of Parameter
   * @exception  Exception  Description of Exception
   * @since
   */
  public CsvReader(String fileName) throws Exception
  {
    this(fileName, ',', false,null);
  }


  /**
   * Insert the method's description here.
   *
   * Creation date: (6-11-2001 15:02:42)
   *
   * @param  fileName                 java.lang.String
   * @param  separator                char
   * @param  suppressHeaders          boolean
   * @exception  java.lang.Exception  The exception description.
   * @since
   */
  public CsvReader(String fileName, char separator, boolean suppressHeaders, String charset)
       throws java.lang.Exception
  {
    this.separator = separator;
    this.suppressHeaders = suppressHeaders;
    this.fileName = fileName;
    this.charset = charset;

    if (charset != null) {
        input = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),charset));
    } else {
        input = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
    }
    // input = new BufferedReader(new FileReader(fileName));
    if (this.suppressHeaders)
    {
      // No column names available. Read first data line and determine number of colums.
      buf = input.readLine();
      String[] data = parseCsvLine(buf);
      columnNames = new String[data.length];
      for (int i = 0; i < data.length; i++)
      {
        columnNames[i] = "COLUMN" + String.valueOf(i+1);
      }
      data = null;
      // throw away.
    }
    else
    {
      String headerLine = input.readLine();
      columnNames = parseCsvLine(headerLine);
    }
  }




  /**
   *Description of the Method
   *
   * @return                Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public boolean next() throws SQLException {
    columns = new String[columnNames.length];
    String dataLine = null;
    try {
        if (suppressHeaders && (buf != null)) {
          // The buffer is not empty yet, so use this first.
          dataLine = buf;
          buf = null;
        } else {
          // read new line of data from input.
          dataLine = input.readLine();
        }
        if (dataLine == null) {
          input.close();
          return false;
        }
    } catch (IOException e) {
        throw new SQLException(e.toString());
    }
    columns = parseCsvLine(dataLine);
    return true;
  }


  /**
   *Description of the Method
   *
   * @since
   */
  public void close()
  {
    try
    {
      input.close();
      buf = null;
    }
    catch (Exception e)
    {
    }
  }


  // This code updated with code by Stuart Mottram to handle line breaks in fields
  // see bug #492063
  protected String[] parseCsvLine(String line) throws SQLException
  {
    Vector values = new Vector();
    boolean inQuotedString = false;
    String value = "";
    String orgLine = line;
    int currentPos = 0;
    int fullLine = 0;

    while (fullLine == 0){
        currentPos = 0;
        line += separator;
        while (currentPos < line.length())
            {
                char currentChar = line.charAt(currentPos);
                if (value.length() == 0 && currentChar == '"' && !inQuotedString)
                    {
                        currentPos++;
                        inQuotedString = true;
                        continue;
                    }
                if (currentChar == '"')
                    {
                        char nextChar = line.charAt(currentPos + 1);
                        if (nextChar == '"')
                            {
                                value += currentChar;
                                currentPos++;
                            }
                        else
                            {
                                if (!inQuotedString)
                                    {
                                        throw new SQLException("Unexpected '\"' in position " + currentPos + ". Line=" + orgLine);
                                    }
                                if (inQuotedString && nextChar != separator)
                                    {
                                        throw new SQLException("Expecting " + separator + " in position " + (currentPos + 1) + ". Line=" + orgLine);
                                    }
                                values.add(value);
                                value = "";
                                inQuotedString = false;
                                currentPos++;
                            }
                    }
                else
                    {
                        if (currentChar == separator)
                            {
                                if (inQuotedString)
                                    {
                                        value += currentChar;
                                    }
                                else
                                    {
                                        values.add(value);
                                        value = "";
                                    }
                            }
                        else
                            {
                                value += currentChar;
                            }
                    }
                currentPos++;
            }
        if (inQuotedString){
            // Remove extra , added at start
            value = value.substring(0,value.length()-1);
            try {
                line = input.readLine();
            } catch (IOException e) {
                throw new SQLException(e.toString());
            }
        } else {
            fullLine = 1;
        }

    }
    String[] retVal = new String[values.size()];
    values.copyInto(retVal);
    return retVal;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -