0

How can I read a binary file and display it in a JTable?

From the code that saves the file like this in Java:

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
    int returnVal = jFileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        try {
            File file = jFileChooser.getSelectedFile();
            this.jTextField.setText(file.getAbsolutePath());
            FileOutputStream fileOut = new FileOutputStream(file);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            
            for(int i=0; i < tblSinhVien.getRowCount(); i++) {
                for(int j=0; j<tblSinhVien.getColumnCount(); j++) {
                    out.writeObject(tblSinhVien.getValueAt(i, j));
                }
            }
            out.close();
            fileOut.close();
            this.jTextField.setText("Đã lưu");
        } catch(IOException e) {
            Logger.getLogger(Student_JFrame.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Use a `DefaultTableModel` then you can serialize it in one call. No need for looping, which is good as at the moment there is nothing in that binary file defining the matrix – g00se Apr 22 '22 at 10:46
  • 1
    A complete example is seen [here](https://stackoverflow.com/a/25526869/230513). – trashgod Apr 22 '22 at 12:20

0 Answers0