Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use ObjectOutput and Input Stream
    text
    copied!<p>I am doing a homework assignment and I am almost finished with it. The last part here is to create a file, read from that file then the contents are used to perform some calculations. According to the API and a couple sites I looked at my syntax is correct but for some reason it is not performing as expected. Any help to steer me in the right direction would be appreciated. I am getting the exception errors for both reading and writing.</p> <pre><code>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"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload