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

📄 csvwriter.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
    Copyright (C) 2002-2003  Together

    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     Sinisa Milosevic
 * @author     Zoran Milakovic
 */

public class CsvWriter
{
  public RandomAccessFile randomCsvFile;
  private File file;
  private String[] columnNames;
  private String[] columnTypes;
  private String[] columns;
  private java.lang.String buf = null;
  private char separator = CsvDriver.DEFAULT_SEPARATOR;
  private long maxFileSize = CsvDriver.DEFAULT_FILE_MAXSIZE;
  private String extension = CsvDriver.DEFAULT_EXTENSION;
  private String tableName;
  private String fileName;
  private int counter;
  private long current;
  private long endLine;


  /**
   * Used with statement.
   *
   * @param fileName
   * @param separator
   * @param extension
   * @param maxFileSize
   * @throws java.lang.Exception
   */
   public CsvWriter(
      String fileName,
      char separator,
      String extension,
      long maxFileSize
      )
      throws java.lang.Exception
  {
    this.separator = separator;
    this.extension = extension;
    this.maxFileSize = maxFileSize;
    if( fileName != null ) {
      this.fileName = fileName;
      this.randomCsvFile = new RandomAccessFile(fileName, "rw");
      this.file = new File(fileName);
      String headerLine = this.randomCsvFile.readLine();
      columnNames = parseCsvLineAsHeader(headerLine);
    }
    counter = 1;
    current = 0;
    endLine=0;
  }


  /**
   * When use split files, this is used when file name is changed.
   * @param name
   * @throws Exception
   */
  public void setFileName(String name) throws Exception {
    this.fileName=name;
    if( this.randomCsvFile != null )
      this.randomCsvFile.close();
    this.file = new File( this.fileName );
    this.randomCsvFile = new RandomAccessFile(this.fileName,"rw");
  }


  public void fillTableColumnNames() throws java.lang.Exception {
    String headerLine = this.randomCsvFile.readLine();
    this.columnNames = parseCsvLineAsHeader(headerLine);
  }

  /**
   * Gets the columnNames attribute of the CsvReader object
   *
   * @return The columnNames value
   * @since
   */
  public String[] getColumnNames()
  {
    return columnNames;
  }

  private String getNextFileName(String currentFileName) {
    String newName = "";
    String number = "";
//name without extension
    String currentFileExtension = currentFileName.substring(currentFileName.lastIndexOf("."), currentFileName.length());
    currentFileName = currentFileName.substring(0, currentFileName.lastIndexOf("."));
    if( currentFileExtension.endsWith(CsvDriver.FILE_NAME_EXT) ) {
       number += currentFileName.substring(currentFileName.length()-3, currentFileName.length());
       long num = Long.valueOf(number).longValue()+1;
       if( num >= 100 && num < 1000 )
         number = String.valueOf( num );
       else if ( num >= 10 && num < 100 )
         number = "0"+String.valueOf( num );
       else if ( num > 1 && num < 10 )
         number = "00"+String.valueOf( num );
       currentFileName = currentFileName.substring(0, currentFileName.length()-3);
       newName = currentFileName + number + currentFileExtension;
    } else {
      newName = currentFileName.toUpperCase() + "001" + this.extension + CsvDriver.FILE_NAME_EXT;
    }
    return newName;
  }


  public String getTableName() {
    if(tableName != null)
      return tableName;

    int lastSlash = 0;
    for(int i = fileName.length()-1; i >= 0; i--)
      if(fileName.charAt(i) == '/' || fileName.charAt(i) == '\\') {
    lastSlash = i;
    break;
      }
      tableName = fileName.substring(lastSlash+1, fileName.length() - 4);
      return tableName;
  }

  /**
   * Get the value of the column at the specified index.
   *
   * @param  columnIndex  Description of Parameter
   * @return              The column value
   * @since
   */

  public String getColumn(int columnIndex)
  {
    return columns[columnIndex];
  }

  /**
   * Get value from column at specified name.
   * If the column name is not found, throw an error.
   *
   * @param  columnName     Description of Parameter
   * @return                The column value
   * @exception  SQLException  Description of Exception
   * @since
   */

  public String getColumn(String columnName) throws SQLException
  {
    for (int loop = 0; loop < columnNames.length; loop++)
    {
      if (columnName.equalsIgnoreCase(columnNames[loop]) || columnName.equalsIgnoreCase(getTableName() + "." + columnNames[loop]))
      {
      return getColumn(loop);
    }
    }
    throw new SQLException("Column '" + columnName + "' not found.");
  }


  /**
   *Description of the Method
   *
   * @return                Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  private long lastCurrent = -1;
  public boolean next() throws SQLException, IOException {
    columns = new String[columnNames.length];
    String dataLine = null;
    if( lastCurrent == -1 ) {
      current = randomCsvFile.getFilePointer();
    } else {
      current = lastCurrent;
    }

    try {
      if (buf != null) {
        // The buffer is not empty yet, so use this first.
        dataLine = buf;
        buf = null;
      } else {
        // read new line of data from input.
        if( lastCurrent != -1 )
          randomCsvFile.seek(lastCurrent);
        dataLine = this.randomCsvFile.readLine();
      }
      if (dataLine == null) {
        String nextFileName = getNextFileName(this.fileName);
          if( new File(nextFileName).exists() ) {
            this.fileName = nextFileName;
            if( randomCsvFile != null )
            	randomCsvFile.close();
            randomCsvFile = new RandomAccessFile(this.fileName, "rw");
            counter = 1;
            current = 0;
            endLine=0;
            //skip header
//            dataLine = input.readLine();
            dataLine = randomCsvFile.readLine();
          } else {
            randomCsvFile.close();
            return false;
          }
      }

    } catch (IOException e) {
      throw new SQLException(e.toString());
    }
    columns = parseCsvLine(dataLine);
    endLine = randomCsvFile.getFilePointer();
    return true;
  }


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


  /**
  *
  * @param line
  * @return array with values or column names.
  * @throws SQLException
  */
 protected String[] parseCsvLine(String line) throws SQLException
 {
   ArrayList values = new ArrayList();
   boolean inQuotedString = false;
   String value = "";
   String orgLine = line;
   int currentPos = 0;
   int fullLine = 0;
   int currentColumn = 0;
   char currentChar;

     line += separator;
     long lineLength = line.length();
     while (fullLine == 0) {
       currentPos = 0;
       while (currentPos < lineLength) {

         //handle BINARY columns
        if( !(this.columnTypes.length <= currentColumn ) ) {
         if (this.columnTypes[currentColumn].equals(CsvDriver.BINARY_TYPE)) {
           String binaryValue = "";
           currentChar = line.charAt(currentPos);
           if (currentChar == ',') {
             values.add(binaryValue); //binary value is null;
             currentPos ++;
           }
           else if (currentChar == '"') {
             if (line.charAt(currentPos + 1) == '"') {
               values.add(binaryValue); //binary value is null
               currentPos = currentPos + 3;
             }
             else {
               // take all until next separator, and that is value
               // do not insert BinaryObject+index into line, just set right currentPos
               // and insert value into vector
               // binary value is always beteween quotes (")
               binaryValue = line.substring(currentPos);
               binaryValue = binaryValue.substring(1,
                                                   binaryValue.indexOf(separator) -
                                                   1);
               values.add(binaryValue);
               currentPos += binaryValue.length() + 3;
             }
           }
           //set currentColumn++
           currentColumn++;
           continue;
         }
        } else {
          throw new SQLException("Invalid csv format : file = "+new File(fileName).getAbsolutePath()+", line = "+line);
       }



//parse one by one character
         currentChar = line.charAt(currentPos);
         if (value.length() == 0 && currentChar == '"' && !inQuotedString) {
//enter here if we are at start of column value
           currentPos++;
           inQuotedString = true;
           continue;
         }

         if (currentChar == '"') {
//get next character
           char nextChar = line.charAt(currentPos + 1);
//if we have "", consider it as ", and add it to value
           if (nextChar == '"') {
             value += currentChar;
             currentPos++;
           }
           else {
//enter here if we are at end of column value
             if (!inQuotedString) {
               throw new SQLException("Unexpected '\"' in position " +
                                      currentPos + ". Line=" + orgLine);
             }

⌨️ 快捷键说明

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