简体   繁体   English

Java - 从文本文件中读取对象

[英]Java - Reading Objects from a text file

I have a program in Java with several classes, these being:我有一个包含多个类的 Java 程序,它们是:

i) Shape ii) Square iii) Rectangle iv) Circle v) Triangle vi) Drawing Canvas vii) Application i) 形状 ii) 方形 iii) 矩形 iv) 圆形 v) 三角形 vi) 绘图画布 vii) 应用

Square, Rectangle, Circle and Triangle all inherit from class Shape Square、Rectangle、Circle 和 Triangle 都继承自Shape

Now, in the Drawing Canvas class, I have two methods, one for writing objects of type Shape to a file and one for reading objects from the file.现在,在Drawing Canvas类中,我有两种方法,一种用于将 Shape 类型的对象写入文件,另一种用于从文件中读取对象。

This is the code:这是代码:

The method which saves to the file works perfectly.保存到文件的方法工作得很好。 However, I am having a problem reading the objects from the file and appending them to the Shapes ArrayList.但是,我在从文件中读取对象并将它们附加到 Shapes ArrayList 时遇到问题。

As it is, the application is given me the error that java.lang.String cannot be cast to shapes_assignment.Shape .事实上,应用程序给了我一个错误,即java.lang.String 无法转换为 shape_assignment.Shape

How can I read the text stored in the text file and use it to recreate the objects once more?如何读取存储在文本文件中的文本并使用它再次重新创建对象? Thanks :)谢谢 :)

You need to write a reader that will take the input string, and make a shape out of it.您需要编写一个读取器来获取输入字符串,并从中生成一个形状。 Here is a simple example to show you how this can work, using the Java Date class .下面是一个简单的示例,它使用Java Date 类向您展示这是如何工作的。 Not having your Shape constructors, I can't write your Shape for you.没有你的 Shape 构造函数,我无法为你编写你的 Shape 。

Date today=new Date();
long var_to_write=today.getTime();

//Save this in to a file, then read it out as long var_in_file

Date previousTime=new Date(var_in_file);

you are adding a String into your ArrayList.您正在将一个字符串添加到您的 ArrayList 中。

            while(input.hasNext())
            {
                temp.add(input.next());
            }

            Shapes.add((Shape)temp.get(i));

here you are trying to cast a String (temp.get(i) will return a String) to Shape type , which is leading to ClassCaseException .在这里您要一个String(temp.get(我)将返回一个字符串)形状类型,这是导致ClassCaseException。

create a Shape instance after you read from the file and populate your ArrayList (temp) with Shape instances.从文件中读取并使用 Shape 实例填充 ArrayList (temp) 后创建一个 Shape 实例。

      List<Shape> temp = new ArrayList<Shape>();
      //read from the scanner and make an Shape object. 
       Shape shape = new Shape(your arguments to the Shape cons);
       now, while adding into shapes you wont need a case, as you made yourself sure that your ArrayList would only accept Shape

       shapes(temp.get(i));

You should use an ObjectOutputStream to write the list of shapes, and retreave them from an ObjectInputStream :您应该使用ObjectOutputStream来编写形状列表,并从ObjectInputStream检索它们:

out.writeObject(Shapes);

Shapes = (ArrayList)in.readObject();

assuming Shapes is an ArrayList .假设Shapes是一个ArrayList

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM