如果从WTable到txt文件的保存数据如下所示,是否有任何简单方法可以加载来自txt文件的数据:
for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
out.println(table.getColumnName(col));
out.print(": ");
out.println(table.getValueAt(row, col));
或者,如果用户退出程序并再次运行,可能是另一种将数据保存在JTable中的方法吗?
非常感谢您的帮助。
答案 0 :(得分:1)
您可以查看序列化JTable。这是将整个对象保存到文件的内置过程。
答案 1 :(得分:1)
从JDK 4开始,您可以使用XMLEncoder
提供永久存储,使用XMLDecoder
重新创建要保存的对象。您需要创建PersistenceDelegate
以帮助编码和解码数据。
以下是DefaultTableModel
:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class DefaultTableModelPersistenceDelegateTest
{
private File file = new File("TableModel.xml");
private final JTextArea textArea = new JTextArea();
private final String[] columnNames = {"Column1", "Column2"};
private final Object[][] data =
{
{"aaa", new Integer(1)},
{"bbb\u2600", new Integer(2)}
};
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
private final JTable table = new JTable(model);
public JComponent makeUI()
{
model.setColumnCount(5);
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
sp.setResizeWeight(.3);
sp.setTopComponent(new JScrollPane(table));
sp.setBottomComponent(new JScrollPane(textArea));
JPanel p = new JPanel();
p.add(new JButton(new AbstractAction("XMLEncoder")
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
XMLEncoder xe = new XMLEncoder(os);
xe.setPersistenceDelegate(DefaultTableModel.class, new DefaultTableModelPersistenceDelegate2());
xe.writeObject(model);
xe.close();
Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
textArea.read(r, null);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}));
p.add(new JButton(new AbstractAction("XMLDecoder")
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
InputStream is = new BufferedInputStream( new FileInputStream( file ));
XMLDecoder xd = new XMLDecoder(is);
model = (DefaultTableModel)xd.readObject();
table.setModel(model);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}));
p.add(new JButton(new AbstractAction("clear")
{
@Override
public void actionPerformed(ActionEvent e)
{
model = new DefaultTableModel();
table.setModel(model);
}
}));
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(sp);
pnl.add(p, BorderLayout.SOUTH);
return pnl;
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override public void run()
{
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new DefaultTableModelPersistenceDelegateTest().makeUI());
f.setSize(420, 340);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
//http://www.oracle.com/technetwork/java/persistence4-140124.html
class DefaultTableModelPersistenceDelegate extends DefaultPersistenceDelegate
{
// Initially creates an empty DefaultTableModel. The columns are created
// and finally each row of data is added to the model.
@Override
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder)
{
DefaultTableModel model= (DefaultTableModel)oldInstance;
Vector<String> columnNames = new Vector<String>(model.getColumnCount());
for (int i = 0; i < model.getColumnCount(); i++)
{
columnNames.add( model.getColumnName(i) );
}
Object[] columnNamesData = new Object[] { columnNames };
encoder.writeStatement(new Statement(oldInstance, "setColumnIdentifiers", columnNamesData));
Vector row = model.getDataVector();
for (int i = 0; i < model.getRowCount(); i++)
{
Object[] rowData = new Object[] { row.get(i) };
encoder.writeStatement(new Statement(oldInstance, "addRow", rowData));
}
}
}
以上代码是在How to write a JTable state with data in xml file using XMLEndcoder in java
中找到的解决方案的更完整示例示例中的文本区域仅用于显示正在保存的数据的XML文件的内容。