保存到文件,不能读取双

Saving to file, can't read double

我有这段代码可以将我的 collection 保存到文件中,它的格式如前所示。

{   
    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 
}

每次当值为 Double

时,我在 nextdouble() 上收到输入不匹配错误

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

scan.useLocale(Locale.US);