简体   繁体   English

我正在尝试制作一个程序,将 object 保存到文件中,然后从文件中读取 object。 我究竟做错了什么?

[英]I'm trying to make a progrm that saves an object to a file and then reads the object from the file. What am I doing wrong?

I think it saves the object because it makes the.txt file but when I run the program, after I input the inputs it outputs "Error initializing stream".我认为它会保存 object,因为它会生成 .txt 文件,但是当我运行程序时,在输入输入后它会输出“初始化流时出错”。 I am new to Java and coding in general so I was just wondering what I am doing wrong.我是 Java 和一般编码的新手,所以我只是想知道我做错了什么。

Here i what I get when I run the code:这是我运行代码时得到的结果:

"What type of costume do you want? “你想要什么类型的服装?

gorilla suit大猩猩套装

What type of mask do you want?你想要什么类型的面膜?

gorilla "大猩猩

This is your outfit.这是你的衣服。

Costume: gorilla suit服装:大猩猩套装

Mask: gorilla面具:大猩猩

Error initializing stream"初始化流时出错”

I would expect the contents of the object ("gorilla suit" and "gorilla") to output on the last line, but instead it outputs "Error initializing stream".我希望 object (“大猩猩套装”和“大猩猩”)的内容在最后一行到 output ,但它输出“错误初始化流”。

Here is the runner class:这是跑步者 class:

class RunHalloween2
{
   public static void main(String[] args)
   {
      Scanner Input = new Scanner(System.in);


      Halloween outfit = new Halloween();

      System.out.println("What type of costume do you want?");
      outfit.setCostume(Input.nextLine());

      System.out.println("What type of mask do you want?");
      outfit.setMask(Input.nextLine());

      System.out.println("");
      System.out.println("This is your outfit.");
      System.out.println("Costume: " + outfit.getCostume());
      System.out.println("Mask:    " + outfit.getMask());


        try {
            FileOutputStream f = new FileOutputStream(new File("myOutfit.txt"));
            ObjectOutputStream o = new ObjectOutputStream(f);

            // Write objects to file
            o.writeObject(outfit);

            o.close();
            f.close();

            FileInputStream fi = new FileInputStream(new File("myOutfit.txt"));
            ObjectInputStream oi = new ObjectInputStream(fi);

            // Read objects

            outfit = (Halloween) oi.readObject();


            System.out.println(outfit.toString());

            oi.close();
            fi.close();

        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("Error initializing stream");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

As per me There can be 2 mistake:-据我所知,可能有两个错误:-

1 - Your file name is myOutfit.txt but it should be myOutfit.ser . 1 - 您的文件名是myOutfit.txt但它应该是myOutfit.ser

2 - Your Halloween class does not implements serilization. 2 - 你的万圣节 class 没有实现序列化。

public class Halloween implements java.io.Serializable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用文件输入从数组创建 object 时我做错了什么 - What am I doing wrong when trying to create an object from an array using file input 尝试将数据写入.txt文件。 我究竟做错了什么? - Trying to write the data to a .txt file. What am I doing wrong? 尝试从文件中读取数据后创建HashMap。 获取空值。 我究竟做错了什么? - Trying to create a HashMap after reading data from file. Getting null values. What am I doing wrong? 我的Java程序在netbeans中运行,但不会在命令中或通过jar文件运行。 我究竟做错了什么? - My java program runs in netbeans, but will not run in command or through the jar file. What am I doing wrong? 尝试使用 LWJGL 中的着色器移动对象时,我做错了什么? - What am I doing wrong when trying to move an object using shaders in LWJGL? 我正在尝试将一个值从 servlet 文件传递​​到 javascript 文件,但我一直为 null 我做错了什么? - I am trying to pass a value from servlet file to javascript file and i am keep getting null where am i doing wrong? 为什么此图形对象无法绘画? (我究竟做错了什么?) - Why is this graphics object not painting? (What am i doing wrong?) 我正在尝试读取一个简单的 Json 文件,但我保存了一个 null。 我究竟做错了什么? 我的 json 仅包含 4 个字段 - I'm trying to read a simple Json file but im getting saved a null. What am i doing wrong? My json contains only 4 fields Android在内部存储上创建文件,我在做什么错? - Android create file on internal storage, what am i doing wrong? 我在做什么错 - 无法从 Main(自定义类)实例化 object - What am I doing wrong - can't instantiate object from Main (Custom class)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM