java - 保存到文件,无法读取双重内容

标签 java collections

我有这段代码将我的 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);

关于java - 保存到文件,无法读取双重内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142559/

相关文章:

java - jsf inputtext 在 bean 中更改值后不更新值

java - hibernate 困惑。 ga、GA 和 Final Releases 之间有什么区别?兼容性?存储库?

java - 我如何通过我在 GWT 中的历史记录来确定我是进入 "forward"还是 "backward"?

iphone - 键值编码@UnionOfObjects

java - LambdaJ forEach 集合

java - 当我打印 map 数据时,为什么我总是按排序顺序排列?

java - Collection 类中的 toArray() 方法

java - 使用单个 ByteArrayOut/InputStream 和 ObjectOut/Input 进行多个操作

java - 如何在 Java + JNI + C++ 进程中查找内存泄漏

javascript - 铁路由器,集合帮助器和 session 变量。如何在JavaScript函数中调用item属性