2016-11-15 112 views
0

我对如何比较2个csv文件(old.csv,change.csv)和执行不同的操作(添加,更新,删除)一个new.csv文件将产生。我只需要一个关于如何做到这一点的想法。我能够使用FileReader和Writer读写文件。我不知道如何比较文件和执行操作。我应该使用数组来输入数据。如何比较2个csv文件,执行操作并输出到一个新的csv文件

我的CSV文件中包含列:

Old.csv 
CODE NAME ADDRESS NUMBER 
0001 John USA  1234 

Change.csv 
CHANGE CODE NAME ADDRESS NUMBER 
ADD  0002 Shane Germany 5678 
UPDATE 0001   Canada 

New.csv 
CODE NAME ADDRESS NUMBER 
0001 John Canada 1234 
0002 Shane Germany 5678 

请的想法帮助。提前致谢。

+0

你可以自己决定文件结构吗?如果是这样,那么我会建议你添加一个ID来标识数据集。否则,只要你有两次相同的名字,你就会遇到问题。或者,是什么号码列的目的?你有没有考虑过将csv导入MySQL数据库?通过这种方式,您可以使用SQL语言来更改数据条目 – Gildraths

+0

主键是CODENUMBER。对不起,我忘了添加。我不应该使用SQL,而是使用数组来输入数据。 – exceptione

回答

0

对于这样的任务,我建议使用CSV解析器像opencsvsupercsv

,并利用数据类的。

下面是一个使用opencsv一个例子:

创建一个类,它看起来是这样的:

public class Person { 

    private String code; 
    private String name; 
    private String address; 
    private String number; 

    public Person(String code, String name, String address, String number) { 
     this.code = code; 
     this.name = name; 
     this.address = address; 
     this.number = number;    
    } 

    public String geCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getNumber() { 
     return number; 
    } 

    public void setNumber(String number) { 
     this.number = number; 
    } 
} 

,并使用上面的类数据持有人在应用程序中比较两个文件,并创建一个新的csv文件

import com.opencsv.CSVReader; 
import com.opencsv.CSVWriter; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.TreeMap; 

public class NewClass1 { 
    public static void main(String[] args) { 
     try { 
      String oldFile = "Old.csv"; 
      String changeFile = "Change.csv"; 
      String newFile = "New.csv"; 
      Map<String,Person> personsList = readOldCsv(oldFile); 
      personsList = getChangesfromChangeCsv(changeFile,personsList); 
      writeNewCsvFile(newFile,personsList); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
    //read your old csv file line by line and create an object person 
    //and put it in a map with code as key and person itself as value 
    //here assumed that your files are tab separeted '\t'; 
    //if not change to ';' or ',' according to your separator 
    public static Map<String,Person> readOldCsv(String fileName) throws IOException{ 
     Map<String,Person> personsList = new HashMap<>(); 
     String [] nextLine; 
     CSVReader reader = new CSVReader(new FileReader(fileName), '\t' ,'\'', 1); 
     while ((nextLine = reader.readNext()) != null) {   
      personsList.put(nextLine[0],new Person(nextLine[0], nextLine[1], nextLine[2], nextLine[3])); 
     }  
     return personsList;   
    } 
    // read your change csv file and add or update persons depending on the change column 
    public static Map<String,Person> getChangesfromChangeCsv(String fileName, Map<String,Person> personsList) throws IOException{    
     String [] nextLine; 
     CSVReader reader = new CSVReader(new FileReader(fileName), '\t' ,'\'', 1); 
     while ((nextLine = reader.readNext()) != null) { 
      switch(nextLine[0].toLowerCase()){ 
       case "add": 
        personsList.put(nextLine[1],new Person(nextLine[1], nextLine[2], nextLine[3], nextLine[4])); 
       case "update": 
        personsList.get(nextLine[1]).setName(!nextLine[2].isEmpty()? nextLine[2]:personsList.get(nextLine[1]).getName()); 
        personsList.get(nextLine[1]).setAddress(!nextLine[3].isEmpty()? nextLine[3]:personsList.get(nextLine[1]).getAddress()); 
        personsList.get(nextLine[1]).setNumber(!nextLine[4].isEmpty()? nextLine[4]:personsList.get(nextLine[1]).getNumber()); 
       default: 
        System.out.println("unexpected entry"); 
      }     
     } 
     // use TreeMap to get a map sorted by key 
     return new TreeMap<>(personsList);   
    } 

    //write the values of the map into a new csv file 
    private static void writeNewCsvFile(String filename,Map<String,Person> personsList) throws IOException {    
     CSVWriter writer = new CSVWriter(new FileWriter(filename,false), '\t',CSVWriter.NO_QUOTE_CHARACTER); 
     for(Person p : personsList.values()){ 
      String[] entries = {p.geCode(),p.getName(),p.getAddress(),p.getNumber()}; 
      writer.writeNext(entries); 
     }     
     writer.close();      
    } 
} 
+0

好吧!我会用这个。非常感谢你!我终于开导了:D – exceptione

0

看起来像这里你的名字列是2个CSV文件之间的主键,在读取CSV文件时你可以创建一个类型的HashMap,其中Name = Name coulmn value和Data是整个对象。稍后在遍历每个更改CSV记录时,您可以使用switch语句来处理更改,并且在每种情况下都可以管理hashmap的各个更新。成功遍历后,你的hashmap将包含必需的更新实例,你可以遍历并转换为新的csv。

相关问题