问答详情
源自:6-1 序列化基本操作

用IDEA默认为utf-8,打印出来的文件老是乱码,怎么处理 呢?

package file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectSeriaDemo {
    public static void main(String[] args) throws IOException {
        String file="F:/Code/Demo6.txt";
        ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file));
        Student student=new Student("01001","张三",20);
        out.writeObject(student);
        out.flush();
        out.close();
    }
}


package file;

import java.io.Serializable;

public class Student implements Serializable {
    private String stuno;
    private String stuname;
    private int stuage;

    public Student(){

    }
    public Student(String stuno,String stuname,int stuage){
        super();
        this.stuno=stuno;
        this.stuname=stuname;
        this.stuage=stuage;

    }

    public String getStuno() {
        return stuno;
    }

    public void setStuno(String stuno) {
        this.stuno = stuno;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public int getStuage() {
        return stuage;
    }

    public void setStuage(int stuage) {
        this.stuage = stuage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuno='" + stuno + '\'' +
                ", stuname='" + stuname + '\'' +
                ", stuage=" + stuage +
                '}';
    }
}


提问者:杨花落尽子规啼 2018-11-26 16:02

个回答

  • 不多bb
    2019-02-20 22:37:39

    把项目编码调成gbk试下

  • qq_何必呢_5
    2018-12-03 20:38:22

    序列化写入文件里的是乱码 但是你从文件读出来的时候就不是乱码了