1

我已经在这段代码上工作了很长一段时间,如果我走上了死胡同,我只想得到简单的提醒。我现在的重点是从不同的 .csv 文件中匹配相同的单元格并将一行复制到另一个 csv 文件中。问题真的是是否可以在特定行写,例如,如果 2 个单元格在第 50 行匹配,我希望写回第 50 行。我假设我可能会将所有内容提取到哈希图中,将其写入然后写回.csv文件?有没有更简单的方法?

例如,我有一个包含人员详细信息的 Csv,而另一个具有实际人员居住地的财产详细信息,我希望将财产详细信息复制到人员 csv,并将它们与正确的人员详细信息相匹配。希望这是有道理的

public class Old {
 public static void main(String [] args) throws IOException
 {
   List<String[]> cols;
   List<String[]> cols1;

   int row =0;
   int count= 0;
   boolean b;
   CsvMapReader Reader = new CsvMapReader(new FileReader("file1.csv"), CsvPreference.EXCEL_PREFERENCE);
   CsvMapReader Reader2 = new CsvMapReader(new FileReader("file2.csv"), CsvPreference.EXCEL_PREFERENCE);

   try {
       cols = readFile("file1.csv");
       cols1 = readFile("fiel2.csv");
       String [] headers = Reader.getCSVHeader(true);

       headers = header(cols1,headers          

           } catch (IOException e) {
       e.printStackTrace();
       return;
   }

   for (int j =1; j<cols.size();j++) //1
   {
       for (int i=1;i<cols1.size();i++){
           if (cols.get(j)[0].equals(cols1.get(i)[0]))
           {


           }
       }

   }

}


private static List<String[]> readFile(String fileName) throws IOException
{
   List<String[]> values = new ArrayList<String[]>();
   Scanner s = new Scanner(new File(fileName));
   while (s.hasNextLine()) {
       String line = s.nextLine();
       values.add(line.split(","));
   }
   return values;
}
public static void csvWriter (String fileName, String [] nameMapping ) throws FileNotFoundException
{
    ICsvListWriter writer = new CsvListWriter(new PrintWriter(fileName),CsvPreference.STANDARD_PREFERENCE);
    try {
        writer.writeHeader(nameMapping);

    } catch (IOException e) {

        e.printStackTrace();
    }

}
public static String[] header(List<String[]> cols1, String[] headers){
    List<String> list = new ArrayList<String>();
    String [] add;
    int count= 0;
    for (int i=0;i<headers.length;i++){
        list.add(headers[i]);
    }

    boolean c;
    c= true;
    while(c)        {           
        add = cols1.get(0);
        list.add(add[count]);
        if (cols1.get(0)[count].equals(null))// this line is never read errpr
        {               
            c=false;
            break;
        } else  
        count ++;

    }

    String[] array = new String[list.size()];
    list.toArray(array);
    return array;

}
4

2 回答 2

1

从您的评论来看,您似乎有以下情况:

  • 文件 1 包含人员
  • 文件 2 包含地址

然后,您希望通过某个键(一个或多个字段)匹配人员和地址,并将组合写回 CSV 文件。

因此,最简单的方法可能是这样的:

//use a LinkedHashMap to preserve the order of the persons as found in file 1
Map<PersonKey, String[]> persons = new LinkedHashMap<>();
//fill in the persons from file 1 here

Map<PersonKey, String[]> addresses = new HashMap<>();
//fill in the addresses from file 2 here

List<String[]> outputLines = new ArrayList<>(persons.size());    
for( Map.Entry<PersonKey, String[]> personEntry: persons.entrySet() ) {
  String[] person = personEntry.getValue();
  String[] address = addresses.get( personEntry.getKey() );

  //merge the two arrays and put them into outputLines
}

//write outputLines to a file

请注意,如果您可以通过一个字段匹配人员和地址,则PersonKey可能只是一个String或一个包装对象(等)。Integer如果您有更多字段,您可能需要一个自定义PersonKey对象equals()hashCode()正确覆盖。

于 2012-08-10T07:32:22.320 回答
1

如果您首先将所有地址和人员详细信息读入内存(正如 Thomas 建议的那样),请小心 - 如果您只处理小型 CSV 文件,那很好,但如果您正在处理,您可能会耗尽内存较大的文件。

作为替代方案,我整理了一个示例,该示例首先读取地址,然后在读取人员详细信息时写入组合的人员/地址详细信息。

只需注意几点:

  • 我使用过 CsvMapReader 和 CsvMapWriter 因为你是 - 这意味着我必须使用包含 Map 的 Map 来存储地址。使用 CsvBeanReader/CsvBeanWriter 会使这更加优雅。

  • 您问题中的代码实际上并未使用 Super CSV 来读取 CSV(您正在使用Scannerand String.split())。如果您的 CSV 在数据中包含逗号(地址很可能),您会遇到问题,因此使用 Super CSV 会更安全,它会为您处理转义的逗号。

例子:

package example;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.supercsv.io.CsvMapReader;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapReader;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;

public class CombiningPersonAndAddress {

    private static final String PERSON_CSV = "id,firstName,lastName\n"
            + "1,philip,fry\n2,amy,wong\n3,hubert,farnsworth";

    private static final String ADDRESS_CSV = "personId,address,country\n"
            + "1,address 1,USA\n2,address 2,UK\n3,address 3,AUS";

    private static final String[] COMBINED_HEADER = new String[] { "id",
            "firstName", "lastName", "address", "country" };

    public static void main(String[] args) throws Exception {

        ICsvMapReader personReader = null;
        ICsvMapReader addressReader = null;
        ICsvMapWriter combinedWriter = null;
        final StringWriter output = new StringWriter();

        try {
            // set up the readers/writer
            personReader = new CsvMapReader(new StringReader(PERSON_CSV),
                    CsvPreference.STANDARD_PREFERENCE);
            addressReader = new CsvMapReader(new StringReader(ADDRESS_CSV),
                    CsvPreference.STANDARD_PREFERENCE);
            combinedWriter = new CsvMapWriter(output,
                    CsvPreference.STANDARD_PREFERENCE);

            // map of personId -> address (inner map is address details)
            final Map<String, Map<String, String>> addresses = 
                    new HashMap<String, Map<String, String>>();

            // read in all of the addresses
            Map<String, String> address;
            final String[] addressHeader = addressReader.getCSVHeader(true);
            while ((address = addressReader.read(addressHeader)) != null) {
                final String personId = address.get("personId");
                addresses.put(personId, address);
            }

            // write the header
            combinedWriter.writeHeader(COMBINED_HEADER);

            // read each person
            Map<String, String> person;
            final String[] personHeader = personReader.getCSVHeader(true);
            while ((person = personReader.read(personHeader)) != null) {

                // copy address details to person if they exist
                final String personId = person.get("id");
                final Map<String, String> personAddress = addresses.get(personId);
                if (personAddress != null) {
                    person.putAll(personAddress);
                }

                // write the combined details
                combinedWriter.write(person, COMBINED_HEADER);
            }

        } finally {
            personReader.close();
            addressReader.close();
            combinedWriter.close();
        }

        // print the output
        System.out.println(output);

    }
}

输出:

id,firstName,lastName,address,country
1,philip,fry,address 1,USA
2,amy,wong,address 2,UK
3,hubert,farnsworth,address 3,AUS
于 2012-08-10T09:13:15.413 回答