3
if(e.getActionCommand().equals("save to file"))
    {
        System.out.println("save is pressed");
        StringBuffer fileContent = new StringBuffer();
        TableModel tModel = m_table.getModel();
        for (int i = 1; i < tModel.getRowCount(); i++) 
        {
         for(int j=0;j<tModel.getColumnCount();j++)
         {
            Object cellValue = tModel.getValueAt(i, j);
            // ... continue to read each cell in a row
            fileContent.append(cellValue);
            // ... continue to append each cell value
            fileContent.append(" ");
         }
         fileContent.append("\n");
        }
        FileWriter fileWriter;
        try {
            fileWriter = new FileWriter(new File("data.txt"));
            fileWriter.write(fileContent.toString());
            fileWriter.flush();
            fileWriter.close();
            }
        catch (IOException e1) 
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

I have created a JDialog in which there is a table. I am able to save the table data into the file on a button click, but what i want to do is to keep that data persisted in the table so when the next time the program is run, that data is available and displayed in the table when a confirm button is pressed. Though I read about Java persistence and Java serialization concepts, I'm not getting the clear idea which technique is appropriate and how to use for this problem.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nidhi
  • 217
  • 1
  • 4
  • 14
  • Edit code into the question. Use code formatting. To do that, select the code and click the `{}` button. – Andrew Thompson Nov 20 '12 at 07:33
  • OK - that is getting better, but please try to figure out how to use code formatting in questions. Read my 1st comment again. Also for better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 20 '12 at 07:54
  • ok..i will do that...and I have posted the code for saving the table data into file but dont know how to retrieve that data and display again in table..If u have any idea it would be helpful for me.. – Nidhi Nov 20 '12 at 08:03
  • *"If u have any idea"* Here are 3: 1) Spell words like 'you' correctly. This is not a text message. 2) **Post an SSCCE.** 3) Reply to the person who's answer has got 2 votes more than any other answer. @trashgod gives good advice. – Andrew Thompson Nov 20 '12 at 08:07
  • I didnt knew that.Thanx for telling. – Nidhi Nov 20 '12 at 08:18

3 Answers3

3

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You are welcome. It's a little hard to show an isolated example. The first one cited is a game; the second is due to @Andrew Thompson. – trashgod Nov 20 '12 at 14:49
1

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 ;-)

André Stannek
  • 7,773
  • 31
  • 52
1

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.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71