txt文件到Jtable数组列表

时间:2018-01-27 22:15:48

标签: arrays arraylist import jtable export

嗨我有一个jtable in witch我想保存并从txtfile加载。 现在我已经实现了,但现在我想以女巫的方式将数据导入已经创建的arraylist。

导入代码

    String filePath = "C:\\Users\\ellim\\Desktop\\TitanPanel\\TitanPanel.txt";
    File file = new File(filePath);

    try {
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        DefaultTableModel model = (DefaultTableModel) tbl_panels.getModel();
        Object[] lines = br.lines().toArray();

        for (int i = 0; i < lines.length; i++) {
            String[] row = lines[i].toString().split(" ");
            model.addRow(row);
        }

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

导出代码

    String filePath = "C:\\Users\\ellim\\Desktop\\TitanPanel\\TitanPanel.txt";
    File file = new File(filePath);
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);

        for (int i = 0; i < tbl_panels.getRowCount(); i++) {
            for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
                bw.write(tbl_panels.getValueAt(i, j).toString() + " ");

            }
            bw.newLine();
        }
        bw.close();
        fw.close();

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

}    

Arraylist我要保存到

ArrayList documentos;

1 个答案:

答案 0 :(得分:0)

这是ArrayList export

的代码
ArrayList<String> documentos = new ArrayList<String>();

// here i am using , as separator 
ArrayListExporter exporter = new ArrayListExporter ( documentos , "," );
exporter.export( tbl_panels );

public interface TableDataExporter {
    public void export( JTable table );
}

public class FileExporter implements TableDataExporter {
    private String fileName;
    private String separator ;
    public FileExporter( String fileName,String separator ) {
        this.fileName=fileName;
        this.separator=separator;
    }
    public void export( JTable table ) {
        File file = new File(fileName);
        try {
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);

            for (int i = 0; i < tbl_panels.getRowCount(); i++) {
                for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
                    if( j != 0 ) {
                        bw.write( separator );
                    }
                    bw.write(tbl_panels.getValueAt(i, j).toString() );

                }
                bw.newLine();
            }
            bw.close();
            fw.close();

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

public class ArrayListExporter implements TableDataExporter {
    private List<String> list;
    private String separator;
    public FileExporter( List<String> list,String separator ) {
        this.list=list;
        this.separator=separator;
    }
    public void export( JTable table ) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < tbl_panels.getRowCount(); i++) {
            builder.delete(0,builder.length());
            for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
                if( j != 0 ) {
                    builder.append( separator );
                }
                Object obj = tbl_panels.getValueAt(i, j);
                builder.append( obj == null ? "null" : obj.toString() );

            }
        }
    }
}