2012-03-21 74 views
0

我在寫這個程序時遇到問題讀取csv並寫入二進制文件

我需要讀取一個csv文件並將其寫入二進制文件,然後再次讀取它。我已經以這種方式被寫成:

Field name Data type 
Name First Text 
Name Last Text 
Birth Day Whole number 
Birth Month Whole number 
Birth Year 4-digit year 
Weight Whole number 
Height Number with decimal part 

我現在該怎麼讀CSV和二進制,而是怎麼做,這是一個謎給我。

什麼toubles我最重要的是,在這個CSV第一行是隻是字符串,然後coninues名姓等

namefirst namelast birthday birthmonth birthyear weight height 

Starlin Castro 243199019072.8 
Madison Bumgarner 18198921576.0 
Jason Heyward 98198924077.3 
Ruben Tejada 2710198916071.2 
Jenrry Mejia 1110198916072.5 
Mike Stanton 811198923577.9 
Dayan Viciedo 103198924071.1 
Chris Sale 303198917077.2 
Freddie Freeman 129198922577.7 
Clayton Kershaw 193198822575.4 
Travis Snider 22198823572.0 
Elvis Andrus 268198820072.0 
Trevor Cahill 13198822076.0 
Rick Porcello 2712198820077.0 
Brett Anderson 12198823576.5 
Fernando Martinez 1010198820073.0 
Jhoulys Chacin 71198821575.2 
Chris Tillman 154198820077.8 
Neftali Feliz 25198821575.5 
Craig Kimbrel 285198820571.6 

你能幫助我嗎?

這是我的代碼至今

我可以一行一行讀,但現在該怎麼繼續?

public static void main(String[] args) { 

try 
{ 

       //csv file containing data 
    String strFile = "BaseballNames1.csv"; 

       //create BufferedReader to read csv file 
    BufferedReader br = new BufferedReader(new FileReader(strFile)); 
    String strLine = ""; 
    StringTokenizer st = null; 
    int lineNumber = 0; 
    int tokenNumber = 0; 

       //read comma separated file line by line 
    while((strLine = br.readLine()) != null) 
    { 
     lineNumber++; 

         //break comma separated line using "," 
     st = new StringTokenizer(strLine, ","); 

     while(st.hasMoreTokens()) 
     { 
           //display csv values 
      tokenNumber++; 
         /*              

      String name = dis.readUTF(); 
      String lastName = dis.readUTF(); 
      int bDay = dis.readInt(); 
      int bMonth = dis.readInt(); 
      int bYear = dis.readInt(); 
      int weight = dis.readInt(); 
      double height = dis.readDouble();   */   



      System.out.println("tokel line:"+st.nextToken()); 




     } 

         //reset token number 
     tokenNumber = 0; 

    } 


} 
    catch(Exception e) 
    { 
     System.out.println("Exception while reading csv file: " + e);     
    } 
} 

回答

0

我在您的輸入中看不到逗號,所以在逗號上打破它可能應該改爲在空格上打破一行。我也看到只有一個大浮點數,其中標題行有五個指定的數字。所以一般來說,你需要澄清發生了什麼事情以及需要做什麼。

告訴我這不是功課。

+0

是的,它是:D沒有逗號,因爲我複製格式化輸出,我只需要和想法如何繼續二進制。在標題中的形式是任務,所以我不明白它......任務是我需要從csv中獲取值,並將其存儲在二進制文件中 – mehnihma 2012-03-21 17:36:23

+0

@mehnihma抱歉,我無法提前幫忙。我現在看到的是,數字243199019072.8可能意味着,1990年第三個月的第24個190磅72.8「(不知道如何)和EG 811198923577.9是8/11/1989 235磅77.9」?如果您再次嘗試使用分隔符完整粘貼輸入,它可能有助於獲得最終答案。 – dlamblin 2012-04-09 19:58:43