2013-10-27 46 views
0

哎Ive得到這裏的代碼塊試圖讀取在.csv文件中的單個行:存儲單CSV線對象成陣列

行=新WarehouseItem [];

public void readCSV(String filename) { 
    FileInputStream fileStrm = null; 
    InputStreamReader rdr; 
    BufferedReader bufRdr; 
    int lineNum; 
    String line; 

    try { 
    fileStrm = new FileInputStream(filename); 
    rdr = new InputStreamReader(fileStrm); 
    bufRdr = new BufferedReader(rdr); 

    numRows = 0; 
    line = bufRdr.readLine(); 
    while (line != null) { 
     rows[numRows] = line; 
     numRows++; 
     line = bufRdr.readLine(); 
    } 
    fileStrm.close(); 
    } 

    catch (IOException e) { 
    if (fileStrm != null) { 
     try { 
    fileStrm.close(); 
     } catch (IOException ex2) {} 
    } 
    System.out.println("Error in file processing: " + e.getMessage()); 
} 
} 

rows[numRows] = line即時試圖行存儲到對象的數組(我有預製包含字符串數組和列的數量的對象)

即時沒有完全確定如何存儲單線即時通訊試圖讀取我的對象。

任何幫助將是非常讚賞:)如果你使用一個CSV庫做這個

+1

rows'的'的聲明從您的代碼段丟失。我們需要看到這一點。 –

+0

你需要告訴我們什麼'行'_is_。它是一個String []嗎?一個String [] []'? –

回答

0

你的生活將是一個可怕的輕鬆很多。使用jackson,將CSV讀入對象數組非常簡單。

例如:

CsvMapper mapper = new CsvMapper(); 
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY); 
File csvFile = new File("input.csv"); // or from String, URL etc 
MappingIterator<Object[]> it = mapper.reader(Object[].class).readValues(csvFile); 

在這裏看到更多的信息在Java的CSV解析:http://demeranville.com/how-not-to-parse-csv-using-java/