2013-02-16 2 views
-1

저는 Java를 처음 사용합니다. 아무도 나에게 7 열의 CSV 파일을 읽는 방법을 제안 할 수 있습니까?java에서 .csv 파일의 7 열을 읽으시겠습니까?

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class main { 

    public static Map<String,String> map1 = null; 
    public static Map<String,String> map2 = null; 

    public static void main(String[] args) { 
     try { 
      readFileandPopulate(); 
      for (Map.Entry<String, String> entry : map1.entrySet()) { 
       System.out.println("Key : " + entry.getKey() + " Value : " 
        + entry.getValue()+" address :"+map2.get(entry.getKey())); 
       //insert into DB 
      } 
     } catch (Exception e) {//Catch exception if any 
      e.printStackTrace(); 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 

    public static void readFileandPopulate() throws Exception { 
     FileInputStream fstream = new FileInputStream("/Users/Desktop/Pattern Recognition/DataSetR/1a19.csv"); 
     // Use DataInputStream to read binary NOT text. 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
     String strLine; 
     map1 = new HashMap<String,String>(); 
     while ((strLine = br.readLine()) != null) { 
      System.out.println(strLine); 
      String temp[] = strLine.split(","); 
      map1.put(temp[0], temp[1]); 
     } 
     in.close(); 
     System.out.println("done 1"); 

    } 
} 

단지 값을 사용하여 하나의 수식을 계산하는 데 몇 열을 읽고 나머지 데이터를 삭제하거나 내가 배열의 모든 값을 저장할 수 및 배열의 ​​인덱스를 사용하기 위해 어떤 방법이 있나요 : 여기

내 코드?

답변

0

http://opencsv.sourceforge.net/과 같은 CSV 파서를 사용하는 것이 좋습니다. 그럼 당신은 할 수있는 다음

CSVReader reader = new CSVReader(new FileReader(filePath), ','); 

List<String[]> rows= reader.readAll(); 

for (String[] row : rows) 
{ 
    for (int i = 0; i < numColumnsToParse; i++) 
    { 
     //do something with row[i] 
    } 
} 
0
import java.io.FileReader; 
import java.io.IOException; 
import java.util.List; 

import au.com.bytecode.opencsv.CSVReader; 

public class ReadCSV 
{ 
    public static void main(String[] args) throws IOException 
    { 
     String[] row = null; 
     String csvFilename = "C:/Users/Hussain/Desktop/data.csv"; 
     CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); 
     List content = csvReader.readAll(); 
     for (Object object : content) 
     { 
      row = (String[]) object; 
      System.out.println("value in row 7 ===>>>"+row[6]); 
     } 
     csvReader.close(); 
     } 
} 

당신이 here

에서 항아리를 추가 할 수 있습니다
관련 문제