繁体   English   中英

读取csv并写入二进制

[英]read csv and write binary

我写这个程序有问题

我需要读取一个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和二进制文件,但是如何做到这一点对我来说却是莫名其妙。

最让我感到困扰的是,在此csv中,第一行只是字符串,然后包含姓氏等等。

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);                  
    }
}

我在输入中看不到逗号,因此应该将逗号分隔改为改为在空格处换行。 我还看到仅一个大的浮点数,其中标题行指定了五个数字。 因此,一般来说,您需要澄清正在发生的事情以及需要解决的问题。

告诉我这不是功课。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM