📄 example20_14.java
字号:
import java.awt.*;import java.awt.event.*;import java.io.*;public class Example20_14 extends Frame implements ActionListener{ TextArea text=null; Button 读入=null,写出=null; FileInputStream file_in=null; FileOutputStream file_out=null; ObjectInputStream object_in=null; //对象输入流。 ObjectOutputStream object_out=null; //对象输出流。 Example20_14() { setLayout(new FlowLayout()); text=new TextArea(6,10); 读入=new Button("读入对象"); 写出=new Button("写出对象"); 读入.addActionListener(this);写出.addActionListener(this); setVisible(true); add(text);add(读入);add(写出); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); pack();setSize(300,300); } public void actionPerformed(ActionEvent e) { if(e.getSource()==写出) { try{ file_out=new FileOutputStream("tom.txt"); object_out=new ObjectOutputStream(file_out);//创建对象输出流。 object_out.writeObject(text); //写对象到文件中。 object_out.close(); } catch(IOException event){} } else if(e.getSource()==读入) { try{ file_in=new FileInputStream("tom.txt"); object_in=new ObjectInputStream(file_in); //创建对象输入流。 TextArea temp=(TextArea)object_in.readObject();//从文件中读入对象。 temp.setBackground(Color.pink); this.add(temp);//添加该对象到窗口。 this.pack();this.setSize(600,600); object_in.close(); } catch(ClassNotFoundException event) { System.out.println("不能读出对象"); } catch(IOException event) { System.out.println("can not read file"); } } } public static void main(String args[]) { Example20_14 win=new Example20_14(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -