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

📄 csvwriter.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
             if (inQuotedString && nextChar != separator) {
               throw new SQLException("Expecting " + separator +
                                      " in position " + (currentPos + 1) +
                                      ". Line=" + orgLine);
             }

//set currentPos to comma after value
             currentPos++;
//if value is empty string between double quotes consider it as empty string
//else if value is empty string between commas consider it as null value
             values.add(value);
             currentColumn++;
             value = "";
             inQuotedString = false;
           }
         }

         else {
//when we are at end of column value, and value is not inside of double quotes
           if (currentChar == separator) {
//when have separator in data
             if (inQuotedString) {
               value += currentChar;
             }
             else {
//if value is empty string between double quotes consider it as empty string
//else if value is empty string between commas consider it as null value
                if( value.equals("") )
                  value = null;
               values.add(value);
               currentColumn++;
               value = "";
             }
           }
           else {
             value += currentChar;
           }
         }

         currentPos++;
       } //end while

       if (inQuotedString) {
         // Remove extra , added at start
         value = value.substring(0, value.length() - 1);
         try {
           line = randomCsvFile.readLine();
         }
         catch (IOException e) {
           throw new SQLException(e.toString());
         }
       }
       else {
         fullLine = 1;
       }

     }// end while( fullLine == 0 )
   String[] retVal = new String[values.size()];
   values.toArray(retVal);

   return retVal;
 }


 /**
  *
  * @param line
  * @return array with values or column names.
  * @throws SQLException
  */
 protected String[] parseCsvLineAsHeader(String line) throws SQLException
 {
   Vector values = new Vector();
   ArrayList columnTypesList = new ArrayList();
   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);
           }
           if (value.endsWith("-"+CsvDriver.BINARY_TYPE)) {
             columnTypesList.add(CsvDriver.BINARY_TYPE);
             value = value.substring(0,value.indexOf("-"+CsvDriver.BINARY_TYPE));
           }
           else
             columnTypesList.add(CsvDriver.VARCHAR_TYPE);
           values.add(value);
           value = "";
           inQuotedString = false;
           currentPos++;
         }
       }
       else {
         if (currentChar == separator) {
           if (inQuotedString) {
             value += currentChar;
           }
           else {
             if (value.endsWith("-"+CsvDriver.BINARY_TYPE)) {
               columnTypesList.add(CsvDriver.BINARY_TYPE);
               value = value.substring(0,
                                       value.indexOf("-"+CsvDriver.BINARY_TYPE));
             }
             else
               columnTypesList.add(CsvDriver.VARCHAR_TYPE);
             values.add(value);
             value = "";
           }
         }
         else {
           value += currentChar;
         }
       }
       currentPos++;
     }
     if (inQuotedString) {
       // Remove extra , added at start
       value = value.substring(0, value.length() - 1);
       try {
         line = randomCsvFile.readLine();
       }
       catch (IOException e) {
         throw new SQLException(e.toString());
       }
     }
     else {
       fullLine = 1;
     }
   }
   String[] retVal = new String[values.size()];
   values.copyInto(retVal);

   this.columnTypes = new String[columnTypesList.size()];
   columnTypesList.toArray(columnTypes);

   return retVal;

 }




  protected boolean newLine(String[] colNames, String[] colValues) throws IOException {
    String newLine="";
    boolean more = true;
    boolean createNewOutput = false;
//find out if file size is out of range, and if so create new file
    while(more) {
      if( (this.maxFileSize != -1) && (this.file.length() > this.maxFileSize) ) {
        String newTableName = this.getNextFileName(this.fileName);
        this.fileName = newTableName;
        this.createExtTable(this.columnNames, this.columnTypes, newTableName);
      } else {
        createNewOutput = true;
        more = false;
      }
    }
    try {
      if (createNewOutput) {
        this.randomCsvFile.close();
        this.randomCsvFile = new RandomAccessFile(this.fileName, "rw");
      }
    }catch(Exception e) {
      e.printStackTrace();
    }

    for(int i=0;i<columnNames.length; i++) {
      boolean find = false;
      for(int j=0;j<colNames.length; j++) {
        if(colNames[j].equalsIgnoreCase(columnNames[i])) {
//          if(colValues[j]==null || colValues[j].equals("null"))
          if(colValues[j]==null)
            newLine=newLine+separator;
          else	{
            newLine=newLine+"\""+colValues[j]+"\""+separator;
          }
          find = true;
        }
      }
      if(!find)
        newLine=newLine+separator;
    }
    if(!newLine.equals(""))
      newLine=newLine.substring(0,newLine.length()-1);

    long l = this.randomCsvFile.length();
    this.randomCsvFile.seek(l);
    this.randomCsvFile.writeBytes("\n"+newLine);
    this.randomCsvFile.close();

    return true;
  }

  protected boolean createTable(String[] colNames, String table) throws IOException {
    String newLine="";
    for(int i=0;i<colNames.length; i++) {
      newLine=newLine+"\""+colNames[i]+"\""+separator;
    }
    if(!newLine.equals(""))
      newLine=newLine.substring(0,newLine.length()-1);

    this.fileName = table;

    this.file = new File( this.fileName );

//    if( !this.file.exists() ) {
      if( this.randomCsvFile != null )
      	this.randomCsvFile.close();
      this.randomCsvFile = new RandomAccessFile(fileName,"rw");
      this.randomCsvFile.writeBytes(newLine);
      this.randomCsvFile.close();
//    }

    return true;
  }

  protected boolean createExtTable(String[] colNames, String[] colTypes, String table) throws IOException {
    String newLine="";
    for(int i=0;i<colNames.length; i++) {
      if(colTypes[i].equals(CsvDriver.BINARY_TYPE))
        newLine=newLine+"\""+colNames[i]+"-"+colTypes[i]+"\""+separator;
      else
        newLine=newLine+"\""+colNames[i]+"\""+separator;
    }
    if(!newLine.equals(""))
      newLine=newLine.substring(0,newLine.length()-1);

    this.fileName = table;

    this.file = new File( this.fileName );

    if( !this.file.exists() ) {
      RandomAccessFile temp = new RandomAccessFile(this.fileName,"rw");
      temp.writeBytes(newLine);
      temp.close();
    }

    return true;
  }


  protected boolean updateFields(
      String[] colNames,
      String[] colValues,
      String[] colWhereNames,
      String[] colWhereValues
      ) throws IOException, SQLException {

    boolean isUpdated=false;

    while( next() ) {
      isUpdated=false;
      counter++;
      boolean find = false;
      out:
      for(int i=0; i<colWhereNames.length; i++) {
//compare values
        if( ! Utils.compareValues( getColumn(colWhereNames[i]), colWhereValues[i] ) )
          break out;
        if(i==(colWhereNames.length-1)) {
          find = true;
        }
      }
//if there is no where clause
      if( colWhereNames.length == 0 )
        find = true;

//go to next line
      lastCurrent = randomCsvFile.getFilePointer();
      if(find) {
        for(int i=0; i < columnNames.length; i++) {
          for(int j=0; j<colNames.length; j++) {
            if(colNames[j].equalsIgnoreCase(columnNames[i]))
              columns[i]=colValues[j];
          }
        }
        String updatedLine = "";
        for(int i=0; i<columns.length; i++) {
          if(columns[i]==null)
            updatedLine=updatedLine+separator;
          else {
            updatedLine=updatedLine+"\""+columns[i]+"\""+separator;
          }

        }
        if(!updatedLine.equals("")) {
          randomCsvFile.seek(endLine);
          String line="";
          String newLine="";

          while(( newLine=randomCsvFile.readLine())!=null ){
            line+=newLine+"\n";
          }
          if(line!=null) {
            if(!line.equals(""))
              line=line.substring(0,line.length()-1);
          }
          updatedLine=updatedLine.substring(0,updatedLine.length()-1);
          randomCsvFile.seek(current);
          randomCsvFile.writeBytes(updatedLine);
          //go to next line
          lastCurrent = randomCsvFile.getFilePointer();
          if( randomCsvFile.getFilePointer() != randomCsvFile.length() ) {
            randomCsvFile.writeBytes("\n");
            //go to next line
            lastCurrent = randomCsvFile.getFilePointer();
            if(line!=null){
              if(!line.equals(""))
                randomCsvFile.writeBytes(line);
              randomCsvFile.setLength(randomCsvFile.getFilePointer());
            }
          }
          isUpdated = false;
        }
      }
    }
    return isUpdated;
  }

}

⌨️ 快捷键说明

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