Results 1 to 2 of 2
  1. #1
    Tzaphiel is offline Member
    Join Date
    Dec 2007
    Rep Power

    Default JTable to txt File

    Hi - I am relatively new to Java and was wondering if anyone could help me with the following issue. I am trying to write a JTable to a txt file, and have the JTable also be able to read the txt file. The problem is that it is writing the letters with a square symbol between each of them and therefore is unable to then read them back into the JTable. Here is my code for saving the file.


    PHP Code:
    	public void saveFile() {
    		File file = null;
    		JFileChooser fc = new JFileChooser();
    		fc.setCurrentDirectory (new File ("."));
    		int result = fc.showSaveDialog (this);
    		if (result == JFileChooser.APPROVE_OPTION) {
    			file = fc.getSelectedFile();
    			if (file.exists ()) {
    				int response = JOptionPane.showConfirmDialog (null,
    					"Overwrite existing file?","Confirm Overwrite",
    				FileOutputStream fos; 
    				DataOutputStream dos;
    				    try {
    				      fos = new FileOutputStream(file);
    				      dos = new DataOutputStream(fos);
    				      for (int i = 0; i <[0].length; i++){;
    				    	  dos.writeChars(table.getValueAt(i, 0).toString().trim());
    				    	  dos.writeChars(table.getValueAt(i, 1).toString().trim());
    				    	  dos.writeChars(table.getValueAt(i, 2).toString().trim());
    				    	  dos.writeChars(table.getValueAt(i, 3).toString().trim());
    				    	  dos.writeChars(table.getValueAt(i, 4).toString().trim());
    				    	  dos.writeChars(table.getValueAt(i, 5).toString().trim());
    				      } // end for
    				    } // end try
    			    	catch (IOException e) {
    			    	} // end catch
                } // end inner if
            else fc.cancelSelection();
    		} // end outer if
    	} // end method saveFile

  2. #2
    sreechithra is offline Member
    Join Date
    Apr 2010
    Rep Power

    Default i tried it

    try using WriteBytes instead of WriteChars. It might work because it worked for me:)

Similar Threads

  1. [SOLVED] JTable DataModel - how to get
    By AZMichael in forum AWT / Swing
    Replies: 3
    Last Post: 07-09-2008, 09:27 AM
  2. ResultSet to JTable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 10:01 AM
  3. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 01:26 PM
  4. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:57 AM
  5. header of jtable
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:11 AM

Posting Permissions

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