2012-03-21 125 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