使用CSVReader和CSVWriter

package CSV;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import com.sun.org.apache.xpath.internal.operations.Equals;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
*
* @author Silence
*/
public class ReadCSV {

    /**
     * @param args the command line arguments
     */
    private static File csvFileRead = new File("C:/dataload/updateCSV/failed_order_load_2103.csv");
   
    private static String  csvFileWritePath="C:/dataload/updateCSV/failed_order_load_2103_update.csv";
   
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        CSVReader reader = new CSVReader(FileReader(file), ',') ; 
        CSVWriter write =new CSVWriter (csvFileWritePath);
        write.writeNext(reader.readNext()); //第一行直接读取之后写出
        String [] nextLine=null;
        while((nextLine = reader.readNext()) != null){
            updateCreditCardType(nextLine); //修改对应的csv文件
            write.writeNext(nextLine);       
        }
        reader.close();
        write.flush();
        write.close();
    }
    public static void updateCreditCardType(String[] line){
       if(line[31].startsWith("37") || line[31].startsWith("34")){
           updateCardType(line,"AE");
       }else if(line[31].startsWith("601")){
           updateCardType(line,"DI");
       }else if(line[31].startsWith("5")){
           updateCardType(line,"MC");
       }else if(line[31].startsWith("4")){
           updateCardType(line,"VS");
       }
    }
    public static void updateCardType(String[] line,String cardType){
         line[6] = cardType;
    }
}

 

你可能感兴趣的:(csv Reader,csvWriter,update csv)