读取.csv文件2 需要导入junit-4.8.2.jar

package com.atpco.textload.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.management.RuntimeErrorException;

public class MockDBDataGenerationUtil {
 private Map<String, ArrayList<String>> statementList;

/**
  * Variable used to find statement ID
  */
 private boolean readFileFromLocalDrive = false;

public MockDBDataGenerationUtil(String csvFileName) throws Exception {
  super();
  setStatementList(csvFileName);
 }

private void setStatementList(String csvFileName) throws Exception {
  if (this.statementList == null) {
   statementList = new HashMap<String, ArrayList<String>>();
  } else {
   statementList.clear();
  }
  createSQLStatements(csvFileName);
 }

private void createSQLStatements(String csvFileName) throws Exception {
  BufferedReader bufRdr = null;
  if (this.readFileFromLocalDrive) {
   bufRdr = getBufferedReaderFormLocal(csvFileName);
  } else {
   bufRdr = getBufferedReader(csvFileName);
  }
  String line = null;
  boolean bNewRecordInStatement = false;
  boolean bVariableNameInNewRecord = false;
  ArrayList<String> currStatement = null;
  String currStatementsID = "";
  String currTableNames = "";
  int size = 0;
  ArrayList<String> currVariableNames = new ArrayList<String>();
  ArrayList<String> currValues = new ArrayList<String>();

while ((line = bufRdr.readLine()) != null) {
   // keep a space for empty value
   String value = " ";
   // System.out.println(line);
   // pass empty line
   // Excel create empty line like
   // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
   String tmp0 = line.replace(‘,‘, ‘ ‘);
   if (tmp0.trim().length() > 0) {
    StringTokenizer st = new StringTokenizer(line, ",");
    int col = 0;
    while (st.hasMoreTokens()) {
     value = st.nextToken();
     if (value.trim().equalsIgnoreCase("STATEMENT ID")) {
      bNewRecordInStatement = false;
      // set statement ID
      value = st.nextToken();
      currStatement = setStatement(value);
      currStatementsID = value;
      size = 0;
      currVariableNames.clear();
      break;
     } else if (value.trim().equalsIgnoreCase("TABLE NAME")) {
      // set table name
      value = st.nextToken();
      // next row will be variable names in a record
      bVariableNameInNewRecord = true;
      currTableNames = value;
      break;
     } else if (bVariableNameInNewRecord) {
      if (col == 0) {
       size = countVariables(line);
      }
      col++;
      if (value == null || value.trim().length() == 0) {
       if (col == size) {
        // last column has space(s)
        size--;
        bVariableNameInNewRecord = false;
       } else {
        // Found empty name
        System.out.println("Found empty name - stop.");
       }
       break;
      }
      value = value.toUpperCase();
      currVariableNames.add(value);
      if (col == size) {
       bVariableNameInNewRecord = false;
      }
     } else {
      // next row will be record data
      bNewRecordInStatement = true;
      currValues.clear();
      break;
     }
    }
    // create a new sql command
    if (bNewRecordInStatement) {
     // the line removed space for empty column that will
     // cause incorrect reading data problem
     String tmp = checkLine(line);
     st = new StringTokenizer(tmp, ",");
     // set record
     for (int i = 0; i < size; i++) {
      value = " ";
      if (st.hasMoreTokens()) {
       value = st.nextToken();
      }
      if (bVariableNameInNewRecord) {
       currVariableNames.add(value);
       if (i == size - 1) {
        // next row will be record data
        bVariableNameInNewRecord = false;
       }
      } else {
       bNewRecordInStatement = true;
       currValues.add(value);
      }
     }
     // create one in current st
     String aSqlStr = createOneSqlInStatement(currStatementsID,
       currTableNames, currVariableNames, currValues);
     if (aSqlStr != null && aSqlStr.length() > 0) {
      currStatement.add(aSqlStr);
     }
    }
   }
  }
  // close the file
  bufRdr.close();
 }

/**
  * This method calculates the RUNDATE based on the Current Date + No. Days
  * passed in argument. The format of this input string is
  * "RUNDATE+<number of days>" where run date is current date.
  *
  * @param value
  * @return DB2 Date format Sring (yyyy-mm-dd)
  */
 public static String calculateRunDate(String value, String inputFormat) {
  String retValue = "";
  int numberOfDays = 0;
  try {
   if (value.indexOf("RUNDATE") != -1) {
    String tmpValue = value.substring(value.indexOf("RUNDATE") + 7,
      value.length()).trim();
    if (!"".equals(tmpValue)) {
     if (tmpValue.indexOf("+") != -1) {
      numberOfDays = new Integer(tmpValue.substring(
        tmpValue.indexOf("+") + 1, tmpValue.length())
        .trim());
     } else if (tmpValue.indexOf("-") != -1) {
      numberOfDays = -new Integer(tmpValue.substring(
        tmpValue.indexOf("-") + 1, tmpValue.length())
        .trim());
     }
    }
   }
   // Get the current date
   // DateTime currDate = new DateTime();
   if (numberOfDays != 0) {
    // Add No. of days to the current date.
    // currDate.addDays(numberOfDays);
   }
   // retValue = currDate.format(inputFormat);
  } catch (Exception ex) {
  }
  return retValue.toString();
 }

private static String calculateRunDateForDB(String value) {
  return "‘" + calculateRunDate(value, "yyyy-MM-dd") + "‘";
 }

/**
  * create SQL statements by using csv file
  *
  * @param fileName
  * @return
  */
 private String createAddSqlStatement(String tbNm, List<String> names,
   List<String> values) {
  String tmpNames = "";
  String tmpValues = "";
  int size = names.size();
  for (int i = 0; i < size; i++) {
   String value = values.get(i);
   String name = names.get(i);
   // ignore empty value
   if (value.trim().length() > 0) {
    value = value.toUpperCase();
    if (value.indexOf("RUNDATE") != -1) {
     value = calculateRunDateForDB(value);
    }
    if (tmpNames.length() > 0) {
     tmpNames += "," + name;
     tmpValues += "," + value;
    } else {
     tmpNames += name;
     tmpValues += value;
    }
   }
  }
  String sqlStr = null;
  if (tmpNames.length() > 0 && tmpNames.length() > 0) {
   // do not create command without data
   sqlStr = "INSERT INTO " + tbNm + "(" + tmpNames + ")VALUES("
     + tmpValues + ");";
  }
  return sqlStr;
 }

private String createOneSqlInStatement(String stID, String currTableNames,
   List<String> currVariableNames, List<String> currValues) {
  String aSql = "";
  // if (stID.indexOf("CREATE") >= 0) {
  aSql = createAddSqlStatement(currTableNames, currVariableNames,
    currValues);
  // }
  return aSql;
 }

/**
  * check line read from csv file the line removed space for empty column
  * that will cause incorrect wrong reading data problem
  *
  * @param line
  * @return String
  */
 // the line removed space for empty column that will cause incorrect
 // wrong reading data problem
 private String checkLine(String line) {
  int pos = line.indexOf(‘,‘);
  int posNext = pos;
  while (pos >= 0) {
   posNext = line.indexOf(‘,‘, pos + 1);
   if (posNext - pos == 1) {
    // reset line "643, ‘ATP1CCQ‘,,08/01/2007"
    // back to "643, ‘ATP1CCQ‘, ,08/01/2007"
    line = line.replace(",,", ", ,");
    pos = line.indexOf(‘,‘, posNext + 1);
   } else {
    pos = posNext;
   }
  }
  return line;
 }

/**
  * count number of variables
  *
  * @param line
  * @return int
  */
 private int countVariables(String line) {
  int count = 0;
  int pos = line.indexOf(‘,‘);
  while (pos >= 0) {
   count++;
   pos = line.indexOf(‘,‘, pos + 1);
  }
  if (count >= 1) {
   // remove empty column(s) at end - e.g. line
   // ="FEET173_SG,CREATE_ID,CREATE_TS,DISUSE_DT,,,,,,,,,,,,,"
   while (line.charAt(line.length() - 1) == ‘,‘) {
    count--;
    line = line.substring(0, line.length() - 1);
   }
   // count last non-empty column at end - e.g. line
   // ="FEET173_SG,CREATE_ID,CREATE_TS,DISUSE_DT"
   if (line.charAt(line.length() - 1) != ‘,‘) {
    count++;
   }
  }
  return count;
 }

/**
  * count number of variables
  *
  * @param line
  * @return int
  */
 private ArrayList<String> setStatement(String stID) {
  // new statement
  ArrayList<String> statement = new ArrayList<String>();
  this.statementList.put(stID, statement);
  return statement;
 }

private BufferedReader getBufferedReader(String csvFileName) {
  BufferedReader bufRdr = null;
  InputStream input = null;
  try {
   input = this.getClass().getResourceAsStream(csvFileName);
  } catch (Exception e) {
   throw new RuntimeErrorException(null, "");
  }
  if (input == null) {
   throw new RuntimeErrorException(null, "");
  }
  bufRdr = new BufferedReader(new InputStreamReader(input));
  return bufRdr;
 }

private BufferedReader getBufferedReaderFormLocal(String csvFileName) {
  BufferedReader bufRdr = null;
  File file = new File(csvFileName);
  try {
   if (!file.exists()) {
    throw new Exception("csv file is not found under" + csvFileName);
   }
   bufRdr = new BufferedReader(new FileReader(file));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

return bufRdr;
 }

// //////////////////////////////////////////////////

/**
  * Executes a group of Insert sql(s) for a statementID - used by JUnit test
  *
  * @param statementID
  * @param jdbcTemplate
  */
 public void exceuteStatement(String statementID) {
  cleanTestData(statementID);
  ArrayList<String> aStatement = this.statementList.get(statementID);
  for (Iterator<String> it = aStatement.iterator(); it.hasNext();) {
   String insertSQL = it.next();
   System.out.println(insertSQL);
   // jdbcTemplate.execute(insertSQL);
  }
 }

/**
  * clean test data to avoid duplication
  *
  * @param JdbcTemplate
  * @param String
  * @return
  */
 public void cleanTestData(String statementID) {
  ArrayList<String> aStatement = this.statementList.get(statementID);
  if (aStatement == null)
   return;
  for (Iterator<String> it = aStatement.iterator(); it.hasNext();) {
   String insertSQL = it.next();
   cleanATestRecode(insertSQL);
  }
 }

/**
  * clean test data to avoid duplication duplication may stop test process.
  * For an insert SQL, create related delete SQL with same data except date
  * and time. Then run the delete before insert process. Example: insert SQL
  * INSERT INTO
  * PARGRP.CXR_FLT_REST_986(CXR_FLT_986_TB_NO,DSUS_DT,D_OCC_CNT,CREATE_ID,
  * CREATE_TS,LAST_MNT_ACT,LAST_MNT_ID,LAST_MNT_TS) VALUES(888885,CURRENT
  * DATE,1,‘ATP1JXD‘,CURRENT TIMESTAMP,‘I‘,‘ATP1JXD‘,CURRENT TIMESTAMP);
  * related delete SQL delete from PARGRP.CXR_FLT_REST_986 where
  * CXR_FLT_986_TB_NO=888885 and D_OCC_CNT=1 and CREATE_ID=‘ATP1JXD‘ and
  * LAST_MNT_ACT=‘I‘ and LAST_MNT_ID=‘ATP1JXD‘
  *
  * @param JdbcTemplate
  * @param String
  * @return
  */
 private void cleanATestRecode(String sql_i) {
  boolean deleteDataFlag = false;
  String sqlStatement = "delete from ";
  int colNameBeginPos = sql_i.indexOf("INSERT INTO ");
  if (colNameBeginPos >= 0) {
   int colNameEndPos = sql_i.indexOf("(");
   // 12 is size of "INSERT INTO "
   if (colNameEndPos >= 12) {
    String tableName = sql_i.substring(colNameBeginPos + 12,
      colNameEndPos);
    if (tableName != null && tableName.length() > 0) {
     sqlStatement += tableName + " where ";
     String valueSeparater = "VALUES(";
     String separater1 = "";
     int colValueEndPos = 0;
     int colValueBeginPos = sql_i.indexOf(valueSeparater) + 1;
     while (colNameEndPos >= 0) {
      // get column name
      colNameBeginPos = colNameEndPos + 1;
      colNameEndPos = sql_i.indexOf(",", colNameBeginPos);
      // logger.debug("  colNameBeginPos="+colNameBeginPos+",colNameEndPos="+colNameEndPos);
      if (colNameEndPos < 0
        || colNameEndPos <= colNameBeginPos) {
       break;
      }
      String name = sql_i.substring(colNameBeginPos,
        colNameEndPos);
      if (name != null && name.length() > 0) {
       // get column value
       colValueEndPos = sql_i.indexOf(",",
         colValueBeginPos);
       if ("VALUES(".equalsIgnoreCase(valueSeparater)) {
        colValueBeginPos += 6;
       }
       if (colValueEndPos < 0
         || colValueEndPos <= colValueBeginPos) {
        // System.out.println("!!!  colValueBeginPos="+colValueBeginPos+",colValueEndPos="+colValueEndPos);
        break;
       }
       // 7 is size of "VALUES("
       String value = sql_i.substring(colValueBeginPos,
         colValueEndPos);
       if (value != null && value.length() > 0) {
        // bypass date created by RUNDATE
        boolean bDate = isDate(value);
        if (!bDate
          && // isDate(value) &&
           // bypass CURRENT date and time
           // value
          !value.equalsIgnoreCase("CURRENT TIMESTAMP")
          && !value
            .equalsIgnoreCase("CURRENT DATE")) {
         sqlStatement += separater1 + name;

sqlStatement += "=" + value;
         deleteDataFlag = true;
        }
        colValueBeginPos = colValueEndPos + 1;
        separater1 = " and ";
        valueSeparater = ",";
       } else {
        break;
       }
      }
     }
    }
   }
  }
  if (deleteDataFlag) {
   System.out.println("run sqlStatement=" + sqlStatement);
   // jdbcTemplate.execute(sqlStatement);
  }
 }

/**
  * Check a date value with format as ‘2010-12-29‘ or ‘2010-12-29 2:47:32 PM‘
  *
  * @param String
  * @return boolean
  */
 private boolean isDate(String value) {
  boolean bDate = false;
  String tmp = value.trim().replaceAll("‘", "");
  if (tmp.trim().length() == 10) {
   tmp = tmp.replaceAll("-", "");
   if (isNumericString(tmp)) {
    bDate = true;
   }
  }
  return bDate;
 }

public static boolean isNumericString(String paramString) {
  try {
   Integer.parseInt(paramString);
   return true;
  } catch (Exception localException) {
  }
  return false;
 }
}

时间: 2024-07-29 05:29:44

读取.csv文件2 需要导入junit-4.8.2.jar的相关文章

Spring Batch示例: 读取CSV文件并写入MySQL数据库

Spring Batch示例: 读取CSV文件并写入MySQL数据库 GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md 原文链接: Reading and writing CVS files with Spring Batch and MySQL 原文作者: Steven Haines - 技术架构师 下载本教程的源代码: S

Spring Batch使用示例: 读取CSV文件并写入MySQL数据库

GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md ------------ 编写批处理程序来处理GB级别数据量无疑是种海啸般难以面对的任务,但我们可以用Spring Batch将其拆解为小块小块的(chunk). Spring Batch 是Spring框架的一个模块,专门设计来对各种类型的文件进行批量处理. 本文先讲解一个简

python-pandas读取mongodb、读取csv文件

续上一篇博客(‘’selenium爬取NBA并将数据存储到MongoDB‘)https://www.cnblogs.com/lutt/p/10810581.html 本篇的内容是将存储到mongo的数据用pandas读取出来,存到CSV文件,然后pandas读取CSV文件. 其中mongo的操作涉及到授权的问题: 如果遇到报错关于authenticate的,需要加授权,用时需要将各参数换成自己的 #导入相应的包 import pymongo import pandas as pd from pa

sparkR读取csv文件

sparkR读取csv文件 The general method for creating SparkDataFrames from data sources is read.df. This method takes in the path for the file to load and the type of data source, and the currently active SparkSession will be used automatically. SparkR suppo

php读取csv文件类

php处理csv文件类: http://www.php100.com/cover/php/540.html <?php define("CSV_Start", 0); define("CSV_Quoted", 1); define("CSV_Quoted2", 2); define("CSV_Unquoted", 3); function readCSV($fh, $len, $delimiter = ',', $enc

读取csv文件,写入oracle数据库

/* * @(#)DataParse.java 2014年4月28日 */ package com.yihaodian.sa.doData; import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.sql.Connection;import java.

用程序读取CSV文件的方法

CSV全称 Comma Separated values,是一种用来存储数据的纯文本文件格式,通常用于电子表格或数据库软件.用Excel或者Numbers都可以导出CSV格式的数据. CSV文件的规则 0 开头是不留空,以行为单位.1 可含或不含列名,含列名则居文件第一行. 2 一行数据不垮行,无空行. 3 以半角符号,作分隔符,列为空也要表达其存在. 4 列内容如存在,,则用""包含起来. 5 列内容如存在""则用""""包

PHP读取csv文件的内容

一次性读取csv文件内所有行的数据 <?php  $file = fopen('windows_2011_s.csv','r');  while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容 //print_r($data); //此为一个数组,要获得每一个数据,访问数组下标即可 $goods_list[] = $data;  } //print_r($goods_list); /* foreach ($goods_list as $arr){     

java读取csv文件

最近用到读取csv文件,以下是源码,读取csv文件,转化为一个String类型的list对象,其中对中午进行了处理,否则会出现乱码,filepath为csv文件的路径 /** * 读取csv文件 */ public static List<String> readCsv(String filepath){ List<String> list = new ArrayList<String>(); String inString = ""; try{ C