Java 如何使用ObjectOutput和InputStream
我正在做一个家庭作业,我快做完了。这里的最后一部分是创建一个文件,从该文件读取内容,然后使用内容执行一些计算。根据API和几个站点,我查看了我的语法是否正确,但由于某些原因,它的性能没有达到预期。如果能帮我指引正确的方向,我将不胜感激。我的阅读和写作都会出现异常错误Java 如何使用ObjectOutput和InputStream,java,file-io,fileinputstream,fileoutputstream,Java,File Io,Fileinputstream,Fileoutputstream,我正在做一个家庭作业,我快做完了。这里的最后一部分是创建一个文件,从该文件读取内容,然后使用内容执行一些计算。根据API和几个站点,我查看了我的语法是否正确,但由于某些原因,它的性能没有达到预期。如果能帮我指引正确的方向,我将不胜感激。我的阅读和写作都会出现异常错误 try{ //In the space below (between Marker 2 and Marker 3) declare an //ObjectOutputStream object ca
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");
}
readObject()的返回值是从流中读取的对象。你把它扔掉了。我应该在readObject()的参数中添加一些东西,这样它才能工作吗?这就是你要告诉我的吗?不,他说的是把readObject()中的值保存到一个变量中。e、 g.
MyObject myobj=readObject()