2014-03-25 4 views
0

나는 여러 줄의 CSV 파일을 가지고 있습니다 .i 줄 단위로 모든 줄을 읽으려고했습니다hashmap을 사용하여 csv 파일 읽기; 출력을 얻지 못했습니다

해시 맵 반복자를 사용합니다. 코드의 2 csv 파일을 읽는 중입니다.

아래와 같이 코드를 작성했습니다.

/**Read csv file line by line 
* 
*/ 
package com.sify.abcd; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 
public class ReadCSV { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     String csvFile = "C:/TestFileProgram/config"; 
     ReadCSV readCSV = new ReadCSV(); 
     readCSV.read(csvFile); 


    } 
    public void read(String fileName) { 
     //Checks if it is a file 
     File file = new File(fileName); 
     if(file.isFile()){   
      if(file.getName().contains(".csv")){ 
       readCSV(file); 
      } 
     } 
     //Checks if it is a directory 
     else if (file.isDirectory()){ 
      System.out.println("Analysing Directory: "+file); 
      String[] list = file.list(); 
      for (String innerFile : list) { 
       read(file+"/"+innerFile); 
      } 
     } 
    } 
    public void readCSV(File cFile) { 
     // TODO Auto-generated method stub 
     BufferedReader br = null; 
     String line = null; 
     String csvSplitBy = ","; 

     try { 
      System.out.println("Reading file: "+cFile); 
      Map<String, String> maps = new HashMap<String, String>(); 

      br = new BufferedReader(new FileReader(cFile)); 
      while ((line = br.readLine()) != null) { 

       //use comma as separator 
       String[] str = line.trim().split(csvSplitBy); 

       for (int i = 0; i < str.length; i++) { 
        String string = str[i]; 
        maps.put(string,line); 
       } 
      } 
      //loop map 
      for (Map.Entry<String, String> entry : maps.entrySet()) { 
       System.out.println(entry.getValue()); 

      } 


     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }  
    } 

} 

출력은 다음과 같습니다.

Analysing Directory: C:\TestFileProgram\com\config 

Analysing Directory: C:\TestFileProgram\com\config\CSV 

Reading file: C:\TestFileProgram\com\config\CSV\test_subtract_2.csv 

test_subtract_2,subtract,0,0,0,0 

Test Condition,Method,expected result,integer1,integer2,integer3 

test_subtract_2,subtract,0,0,0,0 

test_subtract_2,subtract,0,0,0,0 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Reading file: C:\TestFileProgram\com\config\TestAddNumbers.csv 

Test Condition,Method,expected result,integer1,integer2,integer3 

test_add_1`,add,0,0,0, 

test_add_1`,add,0,0,0, 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

Test Condition,Method,expected result,integer1,integer2,integer3 

test_add_1`,add,0,0,0, 

Test Condition,Method,expected result,integer1,integer2,integer3 

선은

Test Condition,Method,expected result,integer1,integer2,integer3 

내 heading.so 내가이 줄 내 경우에는 한 번만 곳 file.and 당은 인쇄 의해 복수의 배를 먼저 인쇄하고 싶어하고있다

test_add_1`,add,0,0,0, 

마지막에 여분의 쉼표가 있습니다. 나는 그것을 원하지 않는다.

친절하게 도와주세요. 사전에

감사합니다 ... 여기

답변

0

는 :

for (int i = 0; i < str.length; i++) { 
    String string = str[i]; 
    maps.put(string,line); 
} 

당신은 각 토큰에 대해 한 번 각 라인을 가하고 있습니다. 헤더에 6 개의 토큰이 있으므로 6 번 나타납니다. test_subtract_2,subtract,0,0,0,0 줄은 토큰을 키로 사용하기 때문에 세 번 나타납니다. 따라서 각 토큰마다 한 줄씩 표시됩니다.

희망이 도움이됩니다.