to persist the data of a dialog box in java

后端 未结 3 1018
天涯浪人
天涯浪人 2021-01-20 19:37
if(e.getActionCommand().equals(\"save to file\"))
    {
        System.out.println(\"save is pressed\");
        StringBuffer fileContent = new StringBuffer();
              


        
相关标签:
3条回答
  • 2021-01-20 19:47

    Consider saving just the TableModel data using java.util.Preferences, exemplified here, or javax.jnlp.PersistenceService, cited here.

    0 讨论(0)
  • 2021-01-20 19:55

    If you don't have access to a database (local or on a server), the best way will probably to use the Serialization.

    Consider using a concrete class to populate your JTable. So to save the data, you just have to make this class to implement Serializable, and then write it in a file using an ObjectOutputStream. Then to retrieve it later use an ObjectInputStream.

    I don't really remember how JTable works with data, but you will probably have to create a custom Model class that inerhits from AbstractTableModel, and will contain the data whether in an array or in a List. But it's better to use a List since it is a lot more fexible and easy to use. So you can direclty serialize the List, and retrieve when program run next time.

    0 讨论(0)
  • 2021-01-20 20:00

    It depends on your application. The easiest and quickest way is object serialization. You can save an object into to a file and load it again. You would do this with the object that holds your table data. There are a lot of tutorials available for that.

    You can extend the concept of serialization by transforming your object in something human readable first (like XML or JSON) and save it as a file then. This has the benefit that you can use the exported data in something other than a Java program. A framework for marshalling to XML is JAXB. Also a lot of tutorials on that.

    Finally you could use a SQL database. A database would be the most performant alternative. A nice framework for persistence in SQL ins Hibernate. It is mostly used for bigger business applications that need a fast data storage backend. Of cause you have to run an SQL server for that one. Again a lot of tutorials available.

    I guess for your case the first alternative would be the best because its quick and easy. SQL would be a little overkill ;-)

    0 讨论(0)
提交回复
热议问题