0

well I'm trying

String filename = "table.file";
          JTable table = new JTable(elements, header);
          TableModel tm = table.getModel();

          try {
               FileOutputStream fos = new FileOutputStream(filename);
               ObjectOutputStream oos = new ObjectOutputStream(fos);
               oos.writeObject(tm);
               oos.flush();
               oos.close();
          } catch (IOException e) {
               e.printStackTrace();
               return;
          }

but it only delete the old entry and write the new one, but I don't want that so I tried appending like

String filename = "table.file";
          JTable table = new JTable(elements, header);
          TableModel tm = table.getModel();

          try {
               FileOutputStream fos = new FileOutputStream(filename,true);
               ObjectOutputStream oos = new ObjectOutputStream(fos);
               oos.writeObject(tm);
               oos.flush();
               oos.close();
          } catch (IOException e) {
               e.printStackTrace();
               return;
          }

but now it's WORST! it don't do any thing! so any ideas?

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by DavidKroukamp
1

well I'm trying

String filename = "table.file";
          JTable table = new JTable(elements, header);
          TableModel tm = table.getModel();

          try {
               FileOutputStream fos = new FileOutputStream(filename);
               ObjectOutputStream oos = new ObjectOutputStream(fos);
               oos.writeObject(tm);
               oos.flush();
               oos.close();
          } catch (IOException e) {
               e.printStackTrace();
               return;
          }

but it only delete the old entry and write the new one, but I don't want that so I tried appending like

String filename = "table.file";
          JTable table = new JTable(elements, header);
          TableModel tm = table.getModel();

          try {
               FileOutputStream fos = new FileOutputStream(filename,true);
               ObjectOutputStream oos = new ObjectOutputStream(fos);
               oos.writeObject(tm);
               oos.flush();
               oos.close();
          } catch (IOException e) {
               e.printStackTrace();
               return;
          }

but now it's WORST! it don't do any thing! so any ideas?

"Here's the trick: subclass ObjectOutputStream and override the writeStreamHeader method:

public class AppendableObjectOutputStream extends ObjectOutputStream {

  public AppendableObjectOutputStream(OutputStream out) {
    super(out);
  }

  @Override
  protected void writeStreamHeader() throws IOException {
    // do not write a header
  }

}

To use it, just check whether the history file exists or not and instantiate either this appendable stream (in case the file exists = we append = we don't want a header) or the original stream (in case the file does not exist = we need a header)." here is the java doc which might be of help-http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html

Edited by DavidKroukamp: n/a

0

well I kind of got the subclass method, but the file history I haven't so please some example codes?

0

well I kind of got the subclass method, but the file history I haven't so please some example codes?

why not just

if(new File("filepath\\filename.fileextension").exists()){
} else {//if file doesnt exist
}
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.