如何将新列添加到CSV文件,并在每列中添加计算的数据?



userid,expDate1,0120152,0220163,032018

我将从每一行中获取expDate,相对于当前月份和年份进行计算,以获取直到到期的月份,然后取整monthsTill(因此,下个月的monthsTill将为1,无论当前日期的第一天还是最后一天月)

userid,expDate,monthsTill1,022017,02,032017,13,042017,23,052017,3

您还将如何“跳过”相应列中具有特定值的行?

最佳答案

根据您的伪代码,这是应采取的措施

private void process(){
    BufferedReader bufferedReader = null;
    PrintWriter p = null;
    String sep = ",";
    String newCol = "monthsTill";
    String defaultTillExpMonth = ">3";
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    int currMonth = cal.get(Calendar.MONTH) + 1;
    try {
    // read csv file
    List<String> input = new ArrayList<String>();
   File inputFile = new File("InputFile");
  bufferedReader = new BufferedReader(new FileReader(inputFile));
    String readLine = "";
    while ((readLine = bufferedReader.readLine()) != null) {
        input.add(readLine);
    }

 // for each row after first row
    // calculate timeTillExpired
        // if format MYYYY (ex 22017)
            // M and YYYY -> MM and YYYY

       // calculate relative to current date as months

    int numOfRecords = input.size();
    if(numOfRecords>1){
        List<String> output = new ArrayList<String>();
        String header = input.get(0) +sep +newCol;
        output.add(header);

        // for each row after first row
        // calculate timeTillExpired
        // if format MYYYY (ex 22017)
              // M and YYYY -> MM and YYYY
        // calculate relative to current date as months
        for(int i=1;i<numOfRecords;i++){
            // I am simply going to get the last column from record
            String row = input.get(i);
            StringBuilder res = new StringBuilder(row);
            String [] entries = row.split(sep);
            int length = entries.length;
            if(length>0){
                res.append(sep);
                String rec = entries[length-1];
                int expMonth = 0;
                // Case of MYYYY. Assumption is it's either MYYYY or MMYYYY
                if(rec.length()==5){
                    expMonth = Integer.valueOf(rec.substring(0, 1));
                } else {
                    expMonth = Integer.valueOf(rec.substring(0, 2));
                }

                int monToExp = expMonth - currMonth;
                // if calculated > 3
                if(monToExp > 3){
                    res.append(defaultTillExpMonth);
                } else {
                    res.append(monToExp);
                }
                output.add(res.toString());
            }
        }
        // Write into the same file.
        // First We'll delete everything in the input file and then write the modified records

        p = new PrintWriter(new FileWriter("output.txt",false));
        // Ouch. Very bad way to handle resources. You should find a better way
        p.print("");
        p.close();
        // Write into file
        p = new PrintWriter(new FileWriter("InputFile"));
        for(String row : output)
        {
            p.println(row);
        }



    } else {
        System.out.println("No records to process");
    }



    } catch(IOException e){
        e.printStackTrace();
    } finally { // Close file
        if(p!=null){
            p.close();
        }
        if(bufferedReader!=null){
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


我强烈建议您了解什么是文件资源,如何有效地处理它们并改进此代码。稍后,您应该转到Apache Commons Library

09-20 08:29