0

我正在做家庭作业,我几乎完成了它。这里的最后一部分是创建一个文件,从该文件中读取,然后使用其中的内容执行一些计算。根据 API 和几个站点,我查看了我的语法是正确的,但由于某种原因,它没有按预期执行。任何能引导我朝着正确方向前进的帮助将不胜感激。我收到了读写的异常错误。

try{
        //In the space below (between Marker 2 and Marker 3) declare an 
        //ObjectOutputStream object called "outFile" for the purpose of 
        //writing Fraction objects into a file called "fraction.out"

        //Marker 2

         FileOutputStream fos = new FileOutputStream("fraction.out");
         ObjectOutputStream outFile = new ObjectOutputStream(fos);


        //Marker 3

        score += 5;

        outFile.writeObject(F[1]);
        outFile.writeObject(F[2]);
        outFile.writeObject(F[3]);
        outFile.close();
    }
    catch(Exception e){
        System.out.println("Could not write objects to file");
    }


    try{
        //In the space below (between Marker 4 and Marker 5) declare an 
        //ObjectInputStream object called "inFile" for the purpose of 
        //reading Fraction objects from a file called "fraction.out"

        //Marker 4

        ObjectInputStream inFile =
                 new ObjectInputStream(new FileInputStream("fraction.out"));


        //Marker 5

        score += 5;


        //In the space below (between Marker 6 and Marker 7) Complete
        //statements that read three fraction objects from the file 
        //as F[5], F[6], and F[7]

        //Marker 6

        f[5] = (Fraction) inFile.readObject();
        f[6] = (Fraction) inFile.readObject();
        f[7] = (Fraction) inFile.readObject();


        //Marker 7
        inFile.close();

        F[4] = F[5].multiply(F[6]).multiply(F[7]);
        score += 5;
        System.out.println("step 12:\tf4 = " + F[4]);
    }
    catch (Exception e){
        System.out.println("Could not read objects from file");

    }
4

1 回答 1

1

readObject() 的返回值是从流中读取的对象。你把它扔掉了。

于 2013-11-14T02:27:16.807 回答