保存到文件,不能读取双倍

时间:2015-01-25 22:55:28

标签: java collections

我有这个代码将我的收藏集保存到文件中,并按照之前的格式进行格式化。

{   
    try {
    PrintWriter save = new PrintWriter(path);
    //writing collection to file 
    for(film f: films)      
    { 
    save.printf("%s%n",f.title);

    save.printf("%d %d %f%n", f.marks, f.numberofmarks, f.average);}

    save.close();
    return true;
    }
    catch (FileNotFoundException e) {return false;}

}

保存文件的结构如下所示:

pokemon

20 7 2.857143

我使用以下代码从文件中读取

{
    //opening scaner and taking care of ioexceptios

    FileReader fr= null;
    Scanner sc = null;

    try {fr = new FileReader(path);} 
    catch (FileNotFoundException e) {return false;}

    sc = new Scanner(fr);

    while(sc.hasNext()){ 
        film tmp = new film();              //creating new object 
        tmp.title = sc.nextLine();          //reading whole line and adding title
        tmp.marks = sc.nextLong();          //2nd line "marks" are read from and added to object
        tmp.numberofmarks = sc.nextLong();  // 2nd value is number of marks ,read and added to object 
        tmp.average = sc.nextDouble();      // last value (3rd) is average,read and added to object

        films.add(tmp);                     //adding the object with read fields to database
         sc.nextLine();                     //moving the scanner to next line

    }

    sc.close();                             //closing scanner
    return true;                            //returning true is everything happened according to plan 
}

nextdouble()

值为Double时,我每次都会在{{1}}上收到错误输入错误

1 个答案:

答案 0 :(得分:0)

几乎可以肯定是语言环境设置。将您的观点转换为逗号,或将以下内容放入您的代码中,然后重试。

scan.useLocale(Locale.US);