在 java 中序列化中获取简单程序的编译错误



这是我的人类

package com.igate.serilaization;
import java.io.Serializable;
public class Person implements Serializable {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Person(String name , int age){
        this.name=name;
        this.age= age;
    }

}

这是我的主要课程

package com.igate.serilaization;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectOutputStream;

public class Serilalizer2 {
    public static void main(String[] args) {
        Person p = new Person("Prasad", 18);
        try {
            FileInputStream fout = new FileInputStream("C:/Users/pmadge/Desktop/serial.ser");
            ObjectOutputStream o = new ObjectOutputStream(fout);
            o.writeObject(p);
            o.close();
            System.out.println("Done");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
我在"ObjectOutputStream o = new ObjectOutputStream(

fout)"上收到编译错误;像"构造函数 ObjectOutputStream(FileInputStream) 未定义"这样的行。

你应该使用FileOutputStream而不是FileInputStream

法典

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

public class Serializer2 {
    public static void main(String[] args) {
        Person p = new Person("Prasad", 18);
        try {
            FileOutputStream fout = new FileOutputStream ("C:/Users/pmadge/Desktop/serial.ser");
            ObjectOutputStream o = new ObjectOutputStream(fout);
            o.writeObject(p);
            o.close();
            System.out.println("Done");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我希望这对你有所帮助。

相关内容

  • 没有找到相关文章

最新更新