【问题标题】:ObjectInputStream/ObjectOutputStream: Errors reading/Writing an appending ObjectsObjectInputStream/ObjectOutputStream:读取/写入附加对象时出错
【发布时间】:2019-06-29 23:09:57
【问题描述】:

我正在用该代码附加 Object。

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class ClaseAppendObjectStream extends ObjectOutputStream 
{
     public ClaseAppendObjectStream(OutputStream os) throws IOException 
     {
        super(os);
     }

     protected void writeStreamHeader() throws IOException 
     {  
         reset();
     }

 }

并且使用 writeObject 方法将它正确地写入我的文件,但是当我将“readObject()”与“objectinputStream”一起使用时。

更多信息: 我使用了“readObjectOverride”(使用子类),它给了我同样的错误。

出现了这个错误:

“无效的流标头:79757200。”

我解决了这个错误,但它错误地读取了“.dat”文件。

我的文件 .dat 有 4 行,但我只读取了 1 行。我的阅读代码是:

ObjectInputStream objetoInStr = new ObjectInputStream(new FileInputStream(sPath))
{
    protected void readStreamHeader() throws IOException 
    {
    }
};

ClassEmployer[] getData = (ClassEmployer[])objetoInStr.readObject();

objetoInStr.close();
String sPhrase="";                      
for(ClassEmployer e : getData )
{
   sPhrase=sPhrase+"Name: " + e.getName() + " Salary: "+ e.getSalary();
}   
objTPane.setText(sPhrase);

它只显示最后一行。

我这样写我的行:

ClassEmployer[] employers= new ClassEmployer[1];
employers[0]= new ClassEm,ployer(objctotext1.getText().trim(),objecttext2.getText().trim());

FileOutputStream objetoFileOutputStream = new FileOutputStream(sPath,true);
BufferedOutputStream objetooutputBuffer = new BufferedOutputStream(objetoFileOutputStream);

ClaseAppendObjectStream objetoOutStr = new ClaseAppendObjectStream(objetooutputBuffer);
objetoOutStr.writeObject(employers) 

【问题讨论】:

    标签: java objectinputstream objectoutputstream


    【解决方案1】:

    我发现我的 owm 误解了。我是读者关于堆栈溢出的其他问题和答案。

    首先我用 AppendClass 正确地编写了我的文件:

     import java.io.IOException;
     import java.io.ObjectOutputStream;
     import java.io.OutputStream;
    
     public class ClaseAppendObjectStream extends ObjectOutputStream 
     {
          public ClaseAppendObjectStream(OutputStream os) throws IOException 
          {
              super(os);
          }
    
         protected void writeStreamHeader() throws IOException 
         {  
              reset();
         } 
    
     }
    

    这样阅读我的文件:

    ObjectInputStream objetoInStr   = new ObjectInputStream(new FileInputStream(sPath))
    {
        protected void readStreamHeader() throws IOException 
        {
        }
    };
    

    最后像这样阅读我的对象:

    ClassEmployer Employer;                     
    
    String sText="";
    try
    {       
        //Infinit reading
        while(true)
        {
            //that code wil have crashed with an EOFEXception
            Employer = (ClasseEmployer)objetoInStr.readObject();
            sText=sText+"Name: " + Employer.getName() + " Salary: "+ Employer.getSalary() +"\n";
        }   
    }
    catch(EOFException ex)
    {
        objetotextoGrande.setText(sText);
    }
    

    所有这些都是解决方案。我希望能帮助像我这样的其他程序员。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2012-10-17
      • 1970-01-01
      • 2021-05-30
      • 2018-05-20
      • 1970-01-01
      • 2018-03-17
      • 2016-04-30
      相关资源
      最近更新 更多