1.//ObjectDemo

package zhangge;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ObjectDemo {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List <Student> list =new ArrayList<Student>();
  
  int num=3;
  menu();
  int choice=-1;
  Scanner sc =new Scanner(System.in); 
  
  System.out.println("请输入选择:");
  choice=sc.nextInt(); 
  while(choice!=3)   
  {
   switch(choice) 
   {  
   case 1:
    add();
    break;
   case 2:
    query();
    break;
    
   default:
    System.out.println("输入选项有错,请重新输入:");
    break;
   }
   menu();
   System.out.println("请输入选择:");
   choice=sc.nextInt();   
  }
  
 }
    public static void menu()
    {
     System.out.println("---------------欢迎进入学生信息管理系统-------------------");
     System.out.println("|                                               |");
     System.out.println("-------------------------------------------------");
     System.out.println("1.添加学生                                      2.查询                                                                         3.退出   ");
     System.out.println("-------------------------------------------------");
    }
    public static void add()
    {
     
     FileOutputStream fil=null;
 
     ObjectOutputStream file=null;
     
     FileInputStream fil1=null;
     ObjectInputStream file1=null;
  try {
   fil1 = new FileInputStream("d://zhangge.txt");
   file1=new ObjectInputStream(fil1);
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     Student student;
     List <Student> list =new ArrayList<Student>();
     try {
   while(true)
   { 
    try
    {
    student =(Student) (file1.readObject());
    list.add(student);
       
    System.out.println("学号   姓名      性别     年龄     专业");
    System.out.println(student.getnumber()+student.getname()+student.getsex()+student.getage()+student.getmajor());
   
    }catch(EOFException e)
    {
       break;
    }
   }
   
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     finally{
      try {
    file1.close();
    fil1.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
      
     }
   
     
     try {
   
   fil = new FileOutputStream("d://zhangge.txt");
   
      file = new ObjectOutputStream(fil);
     
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     Scanner sc = new Scanner(System.in);
        
        int number;
        String name;
        String sex;
        int age;
        String major;  
  System.out.print("输入学号:");
  number=sc.nextInt();
  System.out.print("输入姓名:");
  name=sc.next();
  System.out.print("输入性别:");
  sex=sc.next();
  System.out.print("输入年龄:");
  age=sc.nextInt();
  System.out.print("输入专业:");
  major=sc.next();  
  Student student1 = new Student( number ,  name , sex,  age ,  major);   
  System.out.println("是否保存Y/N");
  String str=null;
  str=sc.next();
  if(str.compareTo("Y")==0)
  {
     
   try {
    list.add(student1);
    for(int i=0;i<list.size();i++)
    {
     file.writeObject(list.get(i));
      
     
    }
    
    System.out.println("学号   姓名      性别     年龄     专业");
    System.out.println(student1.getnumber()+student1.getname()+student1.getsex()+student1.getage()+student1.getmajor());
  
   } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  
   }
   finally{
    try {
     file.close();
     fil.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }
  
  }
  if(str.compareTo("N")==0)
  {
   try {
    
    for(int i=0;i<list.size();i++)
    {
     file.writeObject(list.get(i));
      
     
    }
    
    System.out.println("学号   姓名      性别     年龄     专业");
    System.out.println(student1.getnumber()+student1.getname()+student1.getsex()+student1.getage()+student1.getmajor());
  
   } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  
   }
   finally{
    try {
     file.close();
     fil.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }
  }
  
    }
   
    public static void query()
    {
     FileInputStream fil1=null;
     ObjectInputStream file1=null;
  try {
   fil1 = new FileInputStream("d://zhangge.txt");
   file1=new ObjectInputStream(fil1);
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     Student student;
     List <Student> list =new ArrayList<Student>();
     try {
   while(true)
   {  
    try
    {
    student =(Student) (file1.readObject());
         
    System.out.println("学号   姓名      性别     年龄     专业");
    System.out.println(student.getnumber()+student.getname()+student.getsex()+student.getage()+student.getmajor());
   
    }catch(EOFException e)
    {
     break;
    }
   }
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     finally{
      try {
    file1.close();
    fil1.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
      
     }
    }
   
}

//2.Student 

package zhangge;


import java.io.Serializable;


public class Student implements Serializable{
private int number;
private String name;
private String sex;
private int age;
private String major;

public Student()
{
super();
}

public Student(int number , String name ,String sex, int age , String major )
{
this.number=number;
this.name=name;
this.sex=sex;
this.major=major;
}

public void setnumber(int number)
{
this.number=number;
}
public int getnumber()
{
return this.number;
}
public void setname(String name)
{
this.name=name;
}
public String getname()
{
return this.name;
}
public void setage(int age)
{
this.age=age;
}
public int getage()
{
return this.age;
}
public void setsex(String sex)
{
this.sex=sex;
}
public String getsex()
{
return this.sex;
}
public void setmajor(String major)
{
this.major=major;
}
    public String getmajor()
    {
    return this.major;
    }
}

Logo

快速构建 Web 应用程序

更多推荐