2014-01-15 2 views
-3

나는 field1, field2, field3, field4, field5와 같은 5 개의 헤더 필드를 포함하는 csv 파일을 가지고 있습니다. 파일에 여러 사용자의 헤더가 들어 있습니다. 일부 열은 3 개의 열만 있기 때문에 일부 열은 null 값을 포함 할 수 있습니다.java를 사용하여 헤더를 사용하여 csv 파일을 읽는 방법?

필드 1과 필드 3을 지정한 다음 해당 열의 값만 읽는 것처럼 헤더를 지정하여 열을 읽길 원합니다.

나는 opencsv와 openCSV의 readAll 기능을 시도했다. CSVReader도 사용해 보았습니다. 여기

내 코드입니다 :

public void startScanFile(){ 
String mydemofile=pathtofile; 
BufferedReader br =null; 
try { 
    System.out.println("\nEnter the column number that contains ip : "); 
    br = new BufferedReader(new InputStreamReader(System.in)); 
    ipcolumn = Integer.parseInt(br.readLine()); 
     System.out.println("\nEnter the column number that contains username : "); 
    br = new BufferedReader(new InputStreamReader(System.in)); 
    usercolumn = Integer.parseInt(br.readLine()); 
    System.out.println("\n Enter the column number that contains password"); 
    br = new BufferedReader(new InputStreamReader(System.in)); 
    passcolumn = Integer.parseInt(br.readLine()); 
    }catch(Exception e){ 
} 
try { 
    CSVReader csvReader = new CSVReader(new FileReader(mydemofile)); 
    String[] row; 
    while ((row = csvReader.readNext())!=null){ 
    System.out.println("Col1 is is : " + row[1]); 
    System.out.println("Col3 is : " + row[3]); 
    System.out.println("Col4 is : " + row[4]); 
} 
} catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
} catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
} 

} 내가 지정한 헤더 만의 값을 읽을 수있는 방법

? 당신이 csvreader를 사용하는 경우

+2

지금까지 무엇을 얻었습니까? 또는 적어도 우리에게 파일 내용의 샘플을 보여주십시오 – Baby

+0

[이 기사를 읽으십시오] (http://whathaveyoutried.com) – Barranka

+0

@RafaEl 나는 csv를 읽고 결과로부터 정보를 추출하기 위해 opencsv api와 그 함수 readAll을 사용했습니다. – Vishwas

답변

1

다음 CSV 라이브러리를 제안 할 수 있습니다. 그것의 좋은. 헤더가 포함 된 csv 데이터를 얻는 방법에 대한 예제가 들어있는 아래 URL을 확인하고 데이터의 유효성을 검사 할 수도 있습니다.

http://supercsv.sourceforge.net/examples_reading.html 
관련 문제