Java 将jTable数据导出到文本文件时&;导入回数据的顺序不正确

Java 将jTable数据导出到文本文件时&;导入回数据的顺序不正确,java,swing,jtable,Java,Swing,Jtable,我尝试将JTable数据导出到文本文件,并将数据导入回同一个JTable。但数据在文本文件和JTable中的顺序不正确,如图所示。表中的某些单元格用两个或多个单词填充。帮我解决这个问题是高度赞赏吗?提前谢谢 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import ja

我尝试将JTable数据导出到文本文件,并将数据导入回同一个JTable。但数据在文本文件和JTable中的顺序不正确,如图所示。表中的某些单元格用两个或多个单词填充。帮我解决这个问题是高度赞赏吗?提前谢谢

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class SaveTest1 extends javax.swing.JFrame{
    
    public SaveTest1() {
        initComponents();
        setLocationRelativeTo(null);
    }
    
    @SuppressWarnings("unchecked")                     
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jFileChooser1 = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "Engineering Brick", "Nos", "18.00", "550", "9900.00"},
                {"2", "River Sand", "Cube", "15000.00", "0.1", "1500.00"},
                {"3", "Cement", "cwt", "1100.00", "3", "3300.00"},
                {null, null, null, null, "Cost of Material", "14700.00"}
            },
            new String [] {
                "ID", "Name", "Unit", "Price", "Qty", "Amount"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jButton1.setText("Export");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton1, gridBagConstraints);

        jButton2.setText("Import");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton2, gridBagConstraints);

        pack();
    }                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Save as");
       
        int userSelection = fileChooser.showSaveDialog(this);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = fileChooser.getSelectedFile();

            try {
                FileWriter fw = new FileWriter(fileToSave);
                BufferedWriter bw = new BufferedWriter(fw);

                for (int i = 0; i < jTable1.getColumnCount(); i++) {
                    
                    bw.write(jTable1.getColumnName(i));
                    bw.write("\t");

                }
                for (int i = 0; i < jTable1.getRowCount(); i++) {
                    bw.newLine();
                    for (int j = 0; j < jTable1.getColumnCount(); j++) {
                        bw.write((String) jTable1.getValueAt(i,j));
                        String val = (String) jTable1.getValueAt(i, j);
                        if (val == null) {
                        val = "";
                        }
                        bw.write("\t");
                    }
                }
                JOptionPane.showMessageDialog(this, "File Saved", "Information", JOptionPane.INFORMATION_MESSAGE);
                
                bw.close();
                fw.close();
                
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error", "Error Message", JOptionPane.ERROR_MESSAGE);
            }
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Open");
       
        int userSelection = fileChooser.showOpenDialog(this);
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToOpen = fileChooser.getSelectedFile();
            
            try {
            FileReader fr = new FileReader(fileToOpen);
            BufferedReader br = new BufferedReader(fr);

            DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
            Object[] lines = br.lines().toArray();

            for (int i = 1; i < lines.length; i++) {

                String[] row = lines[i].toString().split(" ");
                model1.addRow(row);
            }

        } catch (Exception ex) {
            Logger.getLogger(SaveTest1.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        model1.setRowCount(0);
    }       
    
    
    public static void main(String args[]) {
       
        
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SaveText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SaveText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SaveText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SaveText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SaveText().setVisible(true);
            }
        });
    }

                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
                   
}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
导入javax.swing.table.DefaultTableModel;
公共类SaveTest1扩展了javax.swing.JFrame{
公共存储测试1(){
初始化组件();
setLocationRelativeTo(空);
}
@抑制警告(“未选中”)
私有组件(){
java.awt.GridBagConstraints GridBagConstraints;
jFileChooser1=newjavax.swing.JFileChooser();
jScrollPane1=newjavax.swing.JScrollPane();
jTable1=newjavax.swing.JTable();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
jButton3=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(新java.awt.GridBagLayout());
jTable1.setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
{“1”、“工程砖”、“编号”、“18.00”、“550”、“9900.00”},
{“2”、“河沙”、“立方体”、“15000.00”、“0.1”、“1500.00”},
{“3”、“水泥”、“cwt”、“1100.00”、“3”、“3300.00”},
{null,null,null,null,“材料成本”,“14700.00”}
},
新字符串[]{
“ID”、“名称”、“单位”、“价格”、“数量”、“金额”
}
) {
类[]类型=新类[]{
java.lang.String.class、java.lang.String.class、java.lang.String.class、java.lang.String.class、java.lang.String.class、java.lang.String.class
};
公共类getColumnClass(int columnIndex){
返回类型[列索引];
}
});
jScrollPane1.setViewportView(jTable1);
gridBagConstraints=new java.awt.gridBagConstraints();
gridBagConstraints.gridx=0;
gridBagConstraints.gridy=0;
gridBagConstraints.gridwidth=2;
gridBagConstraints.fill=java.awt.gridBagConstraints.BOTH;
gridBagConstraints.weightx=0.1;
gridBagConstraints.weighty=0.1;
gridBagConstraints.insets=新java.awt.insets(5,5,5,5);
getContentPane().add(jScrollPane1,gridBagConstraints);
jButton1.setText(“导出”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
gridBagConstraints=new java.awt.gridBagConstraints();
gridBagConstraints.gridx=0;
gridBagConstraints.gridy=1;
gridBagConstraints.weightx=0.1;
gridBagConstraints.insets=新java.awt.insets(5,5,5,5);
getContentPane().add(jButton1,gridBagConstraints);
jButton2.setText(“导入”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
gridBagConstraints=new java.awt.gridBagConstraints();
gridBagConstraints.gridx=1;
gridBagConstraints.gridy=1;
gridBagConstraints.weightx=0.1;
gridBagConstraints.insets=新java.awt.insets(5,5,5,5);
getContentPane().add(jButton2,gridBagConstraints);
包装();
}                       
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
JFileChooser fileChooser=新的JFileChooser();
setDialogTitle(“另存为”);
int userSelection=fileChooser.showsavedilog(此对话框);
if(userSelection==JFileChooser.APPROVE\u选项){
File fileToSave=fileChooser.getSelectedFile();
试一试{
FileWriter fw=新的FileWriter(fileToSave);
BufferedWriter bw=新的BufferedWriter(fw);
对于(int i=0;iString[] row = lines[i].split("\t", -1);
String[] lines = br.lines().toArray(String[]::new);