简体   繁体   English

Java FileStream-ObjectOutputStream ObjectInputStream

[英]Java FileStream - ObjectOutputStream ObjectInputStream

I am creating a little GUI for a "movie manager" with Java and Swing. 我正在使用Java和Swing为“电影管理器”创建一个小GUI。

I have a class MovieTableModel that extends AbstractTableModel and has the data for the rows of the table in an Object[][]data. 我有一个MovieTableModel类,该类扩展了AbstractTableModel,并且在Object [] [] data中具有表行的数据。 A second class MovieUI manages the JFrame and well, the GUI in general. 第二类MovieUI管理JFrame以及一般的GUI。 The last class MovieManager is actually just a main function to create an instance of movieui and make it visible. 最后一个类MovieManager实际上只是创建movieui实例并使之可见的主要功能。

Now my problem is that by now, data is "saved" in my code. 现在我的问题是,到目前为止,数据已“保存”在我的代码中。 I want it to be able to be loaded and saved. 我希望它能够被加载和保存。 If there is no save-file, one should be created and I should be able to add or delete rows of it (the actionlisteners are already set up, I just need a way to handle the file). 如果没有保存文件,则应创建一个保存文件,并且我应该能够添加或删除该文件的行(已经设置了动作侦听器,我只需要一种处理文件的方法)。

So the GUI looks like this: MovieManager 因此GUI如下所示: MovieManager

This is kinda what I tried: 这是我尝试过的:

        File tabledata = new File("tabledata.class");
    if (!tabledata.exists()) {
        try {
            tabledata.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    tabledata.



    FileInputStream in = new FileInputStream("tabledata.class");
    ObjectInputStream input = new ObjectInputStream(in);
    Object data = input.readObject();
    System.out.print(data);

Both didn't work - I think I do understand how this works in general, but I don't know how to make it to the data in my table, especially as it is saved as an Object[][] but the file is an Object. 两者都不起作用-我想我一般都了解它的工作原理,但是我不知道如何将其用于表中的数据,尤其是将其保存为Object [] []却将文件保存为一个东西。 And if that works out - how can I add or delete single rows? 如果可以解决,如何添加或删除单行?

Thanks for your help in advance! 谢谢您的帮助! :) :)

If you serialize you tablemodel or the object that backs your tablemodel, then you can read it later and restore it to the JTable. 如果序列化表模型或支持该表模型的对象,则可以稍后读取它并将其还原到JTable。

When you read the object again from the ObjectInputStream, I think you are missing the cast to the right type you are saving: 当您再次从ObjectInputStream读取对象时,我认为您缺少对要保存的正确类型的强制转换:

FileInputStream in = new FileInputStream("tabledata.class");
ObjectInputStream input = new ObjectInputStream(in);
Object[][] data = (Object[][])input.readObject();

After you create new file you need to write the data using ObjectOutputStream and then read it with ObjectInputStream . 创建新文件后,需要使用ObjectOutputStream写入数据,然后使用ObjectInputStream读取数据。

Object[][] data;
//save
File tabledata = new File("tabledata.dat"); //I wouldn't use class extension (class is for compiled Java)
//creation of file omitted
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(tabledata));
out.writeObject(data);
out.close();

//load
ObjectInputStream  in = new ObjectInputStream(new FileInputStream(tabledata));
data = (Object[][]) in.readObject(); //explict cast required
in.close();

Because this stores all data as one big chunk I don't think it is possible to just read/write one element. 因为这会将所有数据存储为一个大块,所以我认为不可能只读取/写入一个元素。 You would have to resort to counting bytes of stored objects and then skip to right position. 您将不得不求助于存储对象的字节计数,然后跳到正确的位置。 If you would really need to store huge amount of table data I would use some relational database as backend. 如果您确实需要存储大量表数据,则可以使用一些关系数据库作为后端。 Otherwise I wouldn't bother with such optimization. 否则,我不会为这种优化而烦恼。 Just rewrite everything on save. 只要保存就重写所有内容。

暂无
暂无

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

相关问题 Python相当于java ObjectOutputStream和ObjectInputStream? - Python equivalent of java ObjectOutputStream and ObjectInputStream? Java中的ObjectOutputStream / ObjectInputStream中的换行符? - Line Break in an ObjectOutputStream/ObjectInputStream in Java? ObjectInputStream和ObjectOutputStream - ObjectInputStream and ObjectOutputStream Java:尝试使用objectoutputstream / objectinputstream读取对象-崩溃 - Java: Trying to read object with objectoutputstream/objectinputstream - crashes Java从套接字获取ObjectInputStream ObjectOutputStream - Java getting ObjectInputStream ObjectOutputStream from a socket 如何在Java中将CipherInputStream和CipherOutputStream转换为ObjectInputStream和ObjectOutputStream? - How do I convert a CipherInputStream and CipherOutputStream to ObjectInputStream and ObjectOutputStream in Java? 如何使用ObjectOutputStream和ObjectInputStream在Java中处理夏时制? - How to handle daylight saving time in Java using ObjectOutputStream and ObjectInputStream? Java中使用ObjectInputStream/ObjectOutputStream实现网络“包”的优缺点? - Pros and cons for using ObjectInputStream/ObjectOutputStream to implement network “packets” in Java? ObjectInputStream / ObjectOutputStream | 接收和发送大量对象的客户端(Java) - ObjectInputStream / ObjectOutputStream | client that receive and send a lot of objects (Java) 套接字ObjectInputStream和ObjectOutputStream不起作用 - Socket ObjectInputStream and ObjectOutputStream not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM