Hey Lads,

I Have a problem involving my save method ,
basically i want it to actually save a file which is of type JTable
UnFortunetly its not , a Push in the right direction would be great.

save.addMouseListener(new MouseAdapter(){

                  public void mouseClicked(MouseEvent e){
                      JFileChooser fc = new JFileChooser();
                      int returnVal = fc.showSaveDialog(Gav. this);
                      if (returnVal == JFileChooser.APPROVE_OPTION) {
                           File file = fc.getSelectedFile();
                           try {
                            FileWriter fw = new FileWriter(file);

                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                      }
                  }

              });

Recommended Answers

All 5 Replies

Unless the file must be readable by a text editor, you are going to want java.io.DataOutputStream instead of FileWriter. Just go like this:

java.io.DataOutputStream out = new java.io.DataOutputStream(
    new java.io.BufferedOutputStream(new java.io.FileOutputStream(file)));

Then you can use the getModel method on your JTable to get the content of your table and write its rows and columns with nested for loops.

Naturally, only you will be able to read the file you write. You will need to use a java.io.DataInputStream to reverse the process and make any use of the file. Otherwise the file will be useless.

On the other hand, if you really want the file to be a text file, then you should use a java.io.PrintWriter instead of a FileWriter. You can construct a PrintWriter using a file just like you did with FileWriter, but PrintWriter has many more useful methods for writing out data of all kinds as text.

is that how to properly open a file that i have saved ,
thanks very much btw

thanks bguild , got it working

 saveAs.addMouseListener(new MouseAdapter(){
                  public void mouseClicked(MouseEvent e){


                      JFileChooser fc = new JFileChooser();
                      int returnVal = fc.showSaveDialog(Gav.this);
                      if (returnVal == JFileChooser.APPROVE_OPTION) {
                          try {
                              File file = fc.getSelectedFile();
                              PrintWriter hi = new PrintWriter(file);

                              for (int col = 0; col < tm.getColumnCount(); col++) {
                                  hi.print(tm.getColumnName(col) + "\t");
                              }



                              for (int i = 0; i < tm.getRowCount(); i++) {
                                  for (int j = 0; j < tm.getColumnCount(); j++) {
                                      hi.print(tm.getValueAt(i, j).toString() + "\t");

                                  }
                                  hi.println("");
                              }
                              hi.close();

                          } catch (IOException ev) {

                              ev.printStackTrace();
                          }
                      }
                  }});

One small suggestion: To help you read that data back in again you may want to write the number of columns and number of rows at the beginning of the file. Otherwize you won't know how many column names to read before starting to read tha data, or how to divide the data in rows &cols.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.