CSVReader不会检查整个文件

时间:2017-09-25 12:06:09

标签: java csv

我正在尝试使用openCSV打开一个csv文件,遍历每一列,如果userID不同,则在文件末尾写一个新的JavaBean对。

问题是阅读器只检查我文件的第一列而不是整个文件。创建时,该文件仅包含标题,而不包含任何其他内容。程序将检查每一列,如果sudoID不同,它会将其写入文件。如果第一行中的sudoID等于从我的主类导入的sudoID,它将识别它而不是写它。但如果这个-same-sudoID在第二行,它将无法识别它并将再次写入。

例如,如果我的CSV看起来像这样,它将起作用:

"Patient_id Pseudo_ID",
"32415","PAT106663926"

如果看起来像这样,它将重写sudoID:

"Patient_id Pseudo_ID",
"32416","PAT104958880"
"32415","PAT106663926"

谢谢! 我的代码:

public class CSVConnection {

@SuppressWarnings({ "deprecation", "resource", "rawtypes", "unchecked" })
public String getID(String sID,String pseudoID) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException{
    try {
     CsvToBean csv = new CsvToBean();
     String csvFilename = "CsvFile.csv";
     Writer writer= new FileWriter(csvFilename,true);
     CSVReader csvReader = new CSVReader(new FileReader(csvFilename),',','"',1);

      ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
      strategy.setType(PatientCSV.class);
      String[] columns = new String[] {"patID","pseudoID"};
      strategy.setColumnMapping(columns);
     //Set column mapping strategy
      StatefulBeanToCsv<PatientCSV> bc = new StatefulBeanToCsvBuilder<PatientCSV>(writer).withMappingStrategy(strategy).build();
      List patList = csv.parse(strategy, csvReader);

      for (Object patObj : patList) {

          PatientCSV pat = (PatientCSV) patObj;
          if(((PatientCSV) patObj).getPatID().equals(sID)){

              return pat.getPseudoID();
          }
          else
          {  

              PatientCSV pat1 = new PatientCSV();
              pat1.setPatID(sID);
              pat1.setPseudoID(pseudoID);
              patList.add(pat1);
              /*Find a way to import it to the CSV*/
              bc.write(pat1);
              writer.close();
              return pseudoID;
          }
      }


} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

}
    return null;


}




public static void main(String [] args) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException{
    CSVConnection obj = new CSVConnection();
    String sID="32415";
    String pseudoID="PAT101830150";
    obj.getID(sID,pseudoID);
}

}

和Java Bean:

public class PatientCSV {
private String patID;
private String pseudoID;


public String getPatID() {
    return patID;
}


public void setPatID(String patID) {
    this.patID = patID;
}


public String getPseudoID() {
    return pseudoID;
}


public void setPseudoID(String pseudoID) {
    this.pseudoID = pseudoID;
}


public PatientCSV(String patID, String pseudoID) {
    super();
    this.patID = patID;
    this.pseudoID = pseudoID;
}


public PatientCSV() {
    super();
    // TODO Auto-generated constructor stub
}


public String toString()
   {
      return "Patient [id=" + patID + ", pseudoID=" + pseudoID +  "]";
   }

}

1 个答案:

答案 0 :(得分:1)

让我们检查您的for loop

for (Object patObj : patList) {

      PatientCSV pat = (PatientCSV) patObj;
      if(((PatientCSV) patObj).getPatID().equals(sID)){

          return pat.getPseudoID();
      }
      else
      {  

          PatientCSV pat1 = new PatientCSV();
          pat1.setPatID(sID);
          pat1.setPseudoID(pseudoID);
          patList.add(pat1);
          /*Find a way to import it to the CSV*/
          bc.write(pat1);
          writer.close();
          return pseudoID;
      }
  }

所以在你提到的情况下它没有按预期工作,这意味着匹配你输入的行是第二行:

"Patient_id Pseudo_ID",
"32416","PAT104958880"
"32415","PAT106663926"

所以你打电话:getID("32415", "PAT106663926") 你的循环中会发生什么:

  1. 您接受csv患者的第一个元素,id患者:32416
  2. 检查它是否与作为方法输入的id匹配,32415
  3. 它不匹配,因此转到else部分。在那里它创建了新患者(与csv的第2行具有相同的patIDpseudoID)并将其存储在文件中。
  4. 所以到目前为止,您的csv中应该有2个条目具有相同的数据"32415","PAT106663926"
  5. 我认为这是错误,如果匹配,您应该在<{1}}中检查所有条目,然后创建患者并将其存储到csv。< / p>

    一个例子:

    for loop

    P.S。上面的例子编写得很快,只是为了让你知道需要做什么。