Results 1 to 5 of 5
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Arrow want to save JTable in a file..

    i am trying to save JTable in a file..
    which file type is appropriate for this ?

    Java Code:
    table=new JTable(model);
    if (classNameBox.getState()==true){
    File directory = new File("C:/Java/Class");
    directory.mkdirs();
    File file=new File(directory, "newfile.txt");
    file.createNewFile();
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
    oos.writeObject(table);
    oos.close();
    i used txt/xls/doc.....etc

    i am getting strange output.. how can i get right output?
    what we are thinking, it might not be true

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're writing out binary data. Don't try to open the file and view it and expect to see anything but binary data. To make sense out of the data, you need to open your file with an ObjectInputStream.

    Having said this, you still may be better off writing out either the model or the data underlying the model rather than the JTable itself.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    You're writing out binary data.

    in order to write readable characters like a txt-file use

    BufferedWriter out = new BufferedWriter(new FileWriter(aFile));
    Last edited by j2me64; 04-08-2010 at 08:59 PM.

  4. #4
    Fondor is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    As the previous said... your writing binary data.

    You have to get the String of the contents of the JTable. I don't know but you should be able to use something in the vicinity of
    Java Code:
    String.parseString(JTable);
    Then you just need to write that string to the file.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fondor View Post
    As the previous said... your writing binary data.

    You have to get the String of the contents of the JTable. I don't know but you should be able to use something in the vicinity of
    Java Code:
    String.parseString(JTable);
    Then you just need to write that string to the file.
    Did you check what you have written? I guess not because it doesn't make any sense at all. The OP needs to get the TableModel from the JTable. The Model has methods getColumnCount() and getRowCount() as well as a getValueAt( ... ) method. The OP should use those to fetch the data from the model and write it to a file.

    kind regards,

    Jos

Similar Threads

  1. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 11:17 AM
  2. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  3. Save JTable Data
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-08-2010, 12:11 PM
  4. Save file
    By dejos456 in forum New To Java
    Replies: 4
    Last Post: 11-28-2009, 03:13 PM
  5. how to save file..
    By jont717 in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •