我写这个程序有问题
我需要读取一个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);
}
}
答案 0 :(得分:0)
我没有在你的输入中看到逗号,所以在逗号上打破它可能应该改为在空格上打破一行。我还只看到一个大浮点数,其中标题行指定了五个数字。因此,一般来说,您需要澄清正在发生的事情以及需要采取的措施。
告诉我这不是作业。