我正在尝试序列化按钮矩阵(new JButton [6][6]
;),以便我可以将值保存到文件中,然后将它们重新加载到矩阵中。我在网上找到了成功保存数据的代码,但我无法加载数据并将其值返回到矩阵。
我尝试使用以下代码:
public class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent ab) {
saveArray("customlevel", buttons);
}
public void saveArray(String filename, JButton[][] write) {
try {
FileOutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(write);
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
public class LoadListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
loadArray("customlevel", buttons);
}
}
public JButton[][] loadArray(String filename, JButton[][] read) {
try {
FileInputStream fis = new FileInputStream(filename);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
// in.readObject(read);
JButton[][] load = (JButton[][]) in.readObject();
in.close();
return load;
} catch (Exception e) {
System.out.println(e);
}
return null;
}