由於JButton和JTree都已經實現了Serializable接口
方法就是使用ObjectInputStream讀取文件中的對象
如
import java
import java
import java
import java
import java
import java
import javax
import javax
public class Save {
public static void main(String[] args) {
// Write
JButton button = new JButton(
JTree tree = new JTree();
try {
ObjectOutputStream outForButton = new ObjectOutputStream(
new FileOutputStream(
outForButton
outForButton
ObjectOutputStream outForTree = new ObjectOutputStream(
new FileOutputStream(
outForTree
outForTree
} catch (FileNotFoundException e) {
e
} catch (IOException e) {
e
}
// Read
try {
ObjectInputStream inForButton = new ObjectInputStream(
new FileInputStream(
JButton buttonReaded = (JButton) inForButton
ObjectInputStream inForTree = new ObjectInputStream(
new FileInputStream(
JTree treeReaded = (JTree) inForTree
} catch (FileNotFoundException e) {
e
} catch (IOException e) {
e
} catch (ClassNotFoundException e) {
// TODO Auto
e
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25601.html