zoukankan      html  css  js  c++  java
  • ObjectoutputStream和ObjectinputStream的一个不完善的例子

    /**
     * 编写一个Person类,包含序号、姓名、年龄、性别等4个属性,再编写一个Application程序
     * ,采用ObjectOutputStream类Person类的若干个对象实例输出到外部设备的文件上,再采用
     * ObjectInputStream类从外部设备的文件上读取这些对象实例并显示到图形用户界面上。
     * */
    
    /**
     * @author renwenchao
     * @version 1/6/2011
     * */
    
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class javaHomeWork5_2 extends JFrame implements ActionListener{
    
    	Button saveButton;
    	Button openButton;
    	Button showButton;
    	
    	JPanel panel;
    	JTextArea textArea;
    	
    	String str="";
    	
    	Person person1;
    	Person person2;
    	Person person3;
    	Person[] persons;
    	
    	
    	/**
    	 * 构造函数
    	 * */
    	javaHomeWork5_2(){
    		setTitle("renwenchao");
    		setSize(500 , 500);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		panel=new JPanel();
    		textArea=new JTextArea();
    		JScrollPane js=new JScrollPane(textArea);
    		
    		openButton=new Button("OpenFile");
    		saveButton=new Button("SaveFile");
    		showButton=new Button("ShowFile");
    		
    		panel.setLayout(new FlowLayout());
    		panel.add(openButton);
    		panel.add(saveButton);
    		panel.add(showButton);
    		//panel.add(js);
    		panel.add(textArea);
    		add(panel);
    		
    		
    		openButton.addActionListener(this);
    		saveButton.addActionListener(this);
    		showButton.addActionListener(this);
    		
    		 person1=new Person("A",12, 123);
    		 person2=new Person("B",12, 123);
    		 person3=new Person("C",12, 123);
    		 
    		 persons=new Person[3];
    		 persons[0]=person1;
    		 persons[1]=person2;
    		 persons[2]=person3;
    	}
    	
    	/**
    	 * 处理Button事件函数
    	 * */
    	public void actionPerformed(ActionEvent event){
    		if(event.getSource()==openButton){
    			openFile();
    		}
    		
    		if(event.getSource()==saveButton){
    			try {
    				saveFile();
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    		if(event.getSource()==showButton){
    			try {
    				showFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	/**
    	 * 打开文件函数
    	 * */
    	public void openFile(){
    		
    	}
    	
    	/**
    	 * 保存文件函数
    	 * @throws IOException 
    	 * @throws FileNotFoundException 
    	 * */
    	public void saveFile() throws FileNotFoundException, IOException{
    		
    	
    		
    		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
            out.writeObject(persons);
            out.close();
    		
    	}
    	
    	
    	/**
    	 * 在文本域现实内文件内容
    	 * @throws ClassNotFoundException 
    	 * @throws IOException 
    	 * */
    	public void showFile() throws IOException, ClassNotFoundException{
    		
    		ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
             Person[] newPerson = (Person[]) in.readObject();
            
             for(Person per: persons){
            	 str+=per.toString();
            	 str+="\n";
             }
             
             textArea.setText(str);
             in.close();
    	}
    	
    	
    	/**
    	 * 主函数
    	 * */
    	public static void main(String[] args){
    		new javaHomeWork5_2();
    	}
    	
    }
    
    
    
    
    
    
    /**
     * Person类
     * */
     
    class Person implements Serializable{
    	
    	private String name;
    	private int age;
    	private double selary;
    	
    	public Person(){
    		
    	}
    	
    	Person(String name, int age, double selary){
    		this.name=name;
    		this.age=age;
    		this.selary=selary;
    	}
    	
    	public String getName(){
    		return name;
    	}
    	
    	public int getAge(){
    		return age;
    	}
    	
    	public double getSelary(){
    		return selary;
    	}
    }
    


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
    为Android系统的Application Frameworks层增加硬件访问服务
    为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
    为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
    为Android系统内置C可执行程序测试Linux内核驱动程序
    Android内核驱动程序的编写和编译过程
    添加一个Application Framework Service
    getopt()函数
    Google推Android新开发语言Sky:流畅度 秒iOS
    开发人员要避免的七个心态问题
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2067490.html
Copyright © 2011-2022 走看看