如何讀取文件中的對象我使用ObjectInputStream和ObjectOutputStream 類讀取和寫入我的自定義類的學生對象此類演示。java.io.StreamCorruptedException:在java代碼
代碼寫入和讀取使用::
try
{
if(af.filepath==null || af.filepath=="")//file path
{
JOptionPane.showMessageDialog(null, "Please Set File Path", "File Path Error", JOptionPane.ERROR_MESSAGE);
}
else
{
FileOutputStream fs=new FileOutputStream(af.filepath,true);
ObjectOutputStream fo=new ObjectOutputStream(fs);
fo.writeObject(af.s);//write the Super Class Object
fo.close();
}
}
catch (Exception ex)
{
System.out.println(ex);
}
---------------------------------------------------------------------------------
try
{
if(af.filepath==null || af.filepath=="")//file path have whole path of the file
{
JOptionPane.showMessageDialog(null, "Please Set File Path", "File Path Error", JOptionPane.ERROR_MESSAGE);
}
else
{
Student sp;
FileInputStream fs=new FileInputStream(af.filepath);
ObjectInputStream fo=new ObjectInputStream(fs);
while ((sp=(Student)fo.readObject())!=null)
{
sp.set();//for print object
}
fo.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
使用這個我看第一目標文件中,但之後提高
哪裏錯誤上升粘貼錯誤 – Engineer
java.io.StreamCorruptedException:無效的類型代碼:AC 在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356) 在java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) 在MyPackage.MyCustomListener.actionPerformed(AddStudentFrame.java:171) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) –
Visit this [link](http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac)posibl e這將是有益的 – Engineer