碉堡的达gg 2021-05-28 15:03 采纳率: 50%
浏览 760
已采纳

使用ObjectOutputStream写文件时出现乱码,读取也读取不出来是怎么回事?

package serialization;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class WriteObject {

    public static void main(String[] args) {
        System.out.println("我们正在写入Objcet.....");
        
        Person wang = new Person(111,"Wangweida");
        Person yan = new Person(222,"Yanting");
        
        System.out.println(wang);
        System.out.println(yan);
        
    
            try(FileOutputStream fs = new FileOutputStream("person.bin")){
                ObjectOutputStream os = new ObjectOutputStream(fs);
                
                os.writeObject(wang);
                os.writeObject(yan);
                
                os.close();
            } catch (FileNotFoundException e) {
              
                e.printStackTrace();
            } catch (IOException e) {
             
                e.printStackTrace();
            }
    
    }

}

  • 写回答

1条回答 默认 最新

  • 潘宏践踏 2021-05-28 18:03
    关注

    建议看看ObjectOutputStream当中的

    writeObject源码
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 请回答用户的提问 1月30日