2016-09-14 4 views
2

au.com.bytecode.opencsv.CSVReader를 사용하여 CSV 파일을 읽고 모든 레코드를 하나씩 인쇄합니다. 코드가 이상하게 작동합니다. 초기 라인을 제대로 인쇄하고 있습니다. 그러나 그 후에는 한 줄로 여러 줄을 함께 인쇄합니다. 그런 다음 다시 다음 줄 집합을 올바르게 인쇄합니다. CSV 파일을 검사했는데 파일과 관련하여 문제가없는 것으로 나타났습니다. 적절한 형식 파서 (라인 엔딩, 탈출 등)을 구성하는 데 문제가있는 것처럼CSVReader가 여러 줄을 하나의 줄로 처리 중

Next line : [Line1 from the csv file] 
Next line : [Line2 from the csv file] 
Next line : [Line3 from the csv file] 
Next line : [Line4 from the csv file] 
Next line : [Line5 from the csv file] 
Next line : [Line6 from the csv file] 
Next line : [Line7 from the csv file] 
Next line : [Line8 from the csv file] 
Next line : [Line9 from the csv file 
      Line10 from the csv file 
      Line11 from the csv file 
      Line12 from the csv file 
      Line13 from the csv file 
      Line14 from the csv file 
      ] 
Next line : [Line15 from the csv file] 
Next line : [Line16 from the csv file] 
Next line : [Line17 from the csv file] 
Next line : [Line18 from the csv file] 
+0

여기에 몇 가지 문제가있을 수 있습니다. 일부 CR/LF 문자가 누락 될 수 있습니다. – Techidiot

+0

예. 선택 "\ r \ n"또는 "\ n"을 모든 줄의 줄 끝으로 사용하십시오. – gmaslowski

+0

고맙습니다. 하지만 어떻게 확인해야합니까? –

답변

0

보이는 :

 csvReader reader = new CSVReader(new FileReader(fileNameWithLocation), ',', '\"', 1); 

ColumnPositionMappingStrategy<DomainObj> mappingStrategy = 
          new ColumnPositionMappingStrategy<DomainObj>(); 

     mappingStrategy.setType(DomainObj.class);  

      String[] nextLine; 

      while ((nextLine = csvReader.readNext()) != null) 
      { 
        if (nextLine != null) 
        log.debug("Next line : " + Arrays.toString(nextLine)); 
      } 

는 그리고 출력은 같은입니다. 대부분의 경우 자동으로 형식을 감지 할 수 있으므로 univocity-parsers을 시도해보십시오. 또한 opencsv보다 4 배 빠릅니다. 다음 코드를 사용해보십시오.

CsvParserSettings settings = new CsvParserSettings(); 
settings.detectFormatAutomatically(); 

CsvParser parser = new CsvParser(settings); 
parser.beginParsing(fileNameWithLocation); 

String nextLine[]; 
while ((nextLine = parser.parseNext()) != null){ 
    //do stuff 
} 

희망이 있습니다.

면책 조항 : 본인은 본건 저널의 저자입니다. 오픈 소스이며 무료 (Apache 2.0 라이센스)

+0

@ Jeronimo Backes 감사합니다. 나는 이것을 시도하고 어떤 일이 일어나는 지 알려줄 것이다. 또한 내 질문에 대한 마지막 두 번째 의견에서 제기 한 쿼리에 대해 의견을 말 하시겠습니까? –