-
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.
Thanks!
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",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
FileOutputStream fos;
DataOutputStream dos;
try {
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
for (int i = 0; i < Billing.data[0].length; i++){;
dos.writeChars(table.getValueAt(i, 0).toString().trim());
dos.writeChars("\t");
dos.writeChars(table.getValueAt(i, 1).toString().trim());
dos.writeChars("\t");
dos.writeChars(table.getValueAt(i, 2).toString().trim());
dos.writeChars("\t");
dos.writeChars(table.getValueAt(i, 3).toString().trim());
dos.writeChars("\t");
dos.writeChars(table.getValueAt(i, 4).toString().trim());
dos.writeChars("\t");
dos.writeChars(table.getValueAt(i, 5).toString().trim());
dos.writeChars("\n");
} // end for
} // end try
catch (IOException e) {
System.err.println(e.getMessage());
} // end catch
} // end inner if
else fc.cancelSelection();
} // end outer if
} // end method saveFile
-
i tried it
try using WriteBytes instead of WriteChars. It might work because it worked for me:)