⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileevent.java

📁 java学生成绩管理系统.....初学者欢迎下载
💻 JAVA
字号:
 /**
  * FileEvent.java
  *@author weiWang
  *@date 11:53 2006-3-28
  */ 
   
 //文件操作类,存储,删除,修改数据
 package com.herb.server;
 import java.io.*;
 import java.util.*;
   
 public class FileEvent{ 

    /**
     *保存事件,学生信息保存文件名以系名为单位,如果输入学生的系名已存在,则
     *生成一个新的系名文件
     */  
    public static void saveInfor(Student v){
       String major=v.getStudentMajor();
       Vector filenameVector=new Vector();
       filenameVector=getFileName();
       if(filenameVector.contains(major)==false){    //判断是否存在相同的系名,如果存在则不生成一个新的文件名
         filenameVector.add(major);        // 否则生成一个新的一次系名为文件名的存储数据的文件
         saveFileName(filenameVector);    //写入到文件名文件中
       }
          
         Vector studentVector=getStudent(major);   //获取某专业的所有学生信息
         studentVector.add(v);
         try{
           ObjectOutputStream w=new ObjectOutputStream(
               new FileOutputStream("com/herb/server/Infor/student/"+major+".txt"));
              w.writeObject(studentVector);
              w.flush();
              w.close();
         }catch(Exception e){
             System.out.println(e.getMessage());
         } 
       
     }

    //保存文件名
    public static void saveFileName(Vector m){
       try{
        ObjectOutputStream w=new ObjectOutputStream(
           new FileOutputStream("com/herb/server/Infor/fileName/FileNameTable.txt"));
        w.writeObject(m);
        w.flush();
        w.close();  
       }catch(Exception e){
           System.out.println(e.getMessage());
       }
    }        
             
    //获得文件名向量
    public static Vector getFileName(){ 
       Vector v=new Vector();
       try{  
         ObjectInputStream in=new ObjectInputStream(
            new FileInputStream("com/herb/server/Infor/fileName/FileNameTable.txt"));
         v=(Vector)(in.readObject());
       }catch(Exception e){
            System.out.println(e.getMessage());
       }
       return v;
    }     
 
    //获得学生信息
    public static Vector getStudent(String ma){ 
      Vector v=new Vector(); 
      try{ 
          ObjectInputStream in=new ObjectInputStream(  
             new FileInputStream("com/herb/server/Infor/student/"+ma+".txt"));
          v=(Vector)in.readObject();
      }catch(Exception e){
          System.out.println(e.getMessage());
      }
      return v;
    }  
    
   //替换某学生信息
    public static void resetStudentInfor(Student s,String m,int row){
            Vector v=getStudent(m);
            v.setElementAt(s,row);
            saveStudentInfor(m,v);
    }
     
    //将学生信息写入相应的文件内
    public static void saveStudentInfor(String m,Vector v){
        try{
           ObjectOutputStream out=new ObjectOutputStream( 
              new FileOutputStream("com/herb/server/Infor/student/"+m+".txt"));
           out.writeObject(v);
        }catch(Exception e){
           System.out.println(e.getMessage());
        }
    }

    //删除学生信息
    public static void deleteStudent(String m,int row){
       Vector del=new Vector();
       del=getStudent(m);
       del.remove(row);
       saveStudentInfor(m,del);    
    }       
   
   //修改学生信息
    public static void resetStudentInfor(String major,int row,String mark){
       Vector v=getStudent(major);  
       String i=((Student)(v.elementAt(row))).getStudentId();
       String n=((Student)(v.elementAt(row))).getStudentName();
       String s=((Student)(v.elementAt(row))).getStudentSex();
       String m=((Student)(v.elementAt(row))).getStudentMajor();
       String c=((Student)(v.elementAt(row))).getStudentClass();
       String l=((Student)(v.elementAt(row))).getStudentLesson();
       String k=mark;
       String p=((Student)(v.elementAt(row))).getStudentPhone(); 
       Student student=new Student(i,n,s,m,c,l,k,p);
       v.set(row,student);     // 修改第row行的元素为修改后的student 
       saveStudentInfor(major,v);
       System.out.println(v.toString());
    }
   
   //存储聊天内容
    public static void saveChatMsg(String msg,String who){
       Vector v=new Vector();
       v.add(msg);
       v.add(who);
       Vector vv=getChatMsg();
       vv.add(v);
       try{
          ObjectOutputStream w=new ObjectOutputStream(
             new FileOutputStream("com/herb/server/Infor/chat/Chatmessage.txt")); 
          w.writeObject(vv);
       }catch(Exception e){
          System.out.println(e.getMessage());
       }   
    }
 
   //获得聊天内容
    public static Vector getChatMsg(){
       Vector v=new Vector();
       try{
          ObjectInputStream r=new ObjectInputStream(
             new FileInputStream("com/herb/server/Infor/chat/Chatmessage.txt"));
          v=(Vector)r.readObject();
       }catch(Exception e){
          System.out.println(e.getMessage());
       }
       return v;
    }
   
    //存储聊天客人信息
    public static void saveChatterInfor(Vector v){
        try{
          ObjectOutputStream w=new ObjectOutputStream(
             new FileOutputStream("com/herb/server/Infor/chat/ChatterInfor.txt")); 
          w.writeObject(v);
       }catch(Exception e){
          System.out.println(e.getMessage());
       }   
    }

    //获得聊天客人信息
    public static Vector getChatterInfor(){
       Vector v=new Vector();
       try{
          ObjectInputStream r=new ObjectInputStream(
             new FileInputStream("com/herb/server/Infor/chat/ChatterInfor.txt"));
          v=(Vector)r.readObject();
       }catch(Exception e){
          System.out.println(e.getMessage());
       }
       return v;
    }

    //获得历史信息
    public static Vector getHistoryInfor(){
        Vector history=new Vector();
        String s=new String();
        try{
          ObjectInputStream in=new ObjectInputStream(
             new FileInputStream("com/herb/server/Infor/History/HistoryInformation.txt"));
          history=(Vector)in.readObject();
          in.close();
        }catch(Exception e){
             System.out.println(e.getMessage());
        }
        return history;
     }   

    //写入历史信息
     public static void saveHistoryInfor(String infor,int code){
          Vector v;
          v=getHistoryInfor();    //获得数据先
          String historyInfor=new String();
          if(code==0)             // 新增学生操作代号
             historyInfor="新增学生: "+infor;
          if(code==1)        
             historyInfor="删除学生: "+infor;
          if(code==2)
             historyInfor="修改学生: "+infor+"信息";
          if(code==3)
             historyInfor="新增专业:  "+infor;
          if(code==4)
             historyInfor="删除专业: "+infor; 
          v.add(historyInfor);
          try{
          ObjectOutputStream w=new ObjectOutputStream(
             new FileOutputStream("com/herb/server/Infor/History/HistoryInformation.txt")); 
               w.writeObject(v);
          }catch(Exception e){
               System.out.println(e.getMessage());
          }   
     }    
 
    // 删除历史纪录
       public static void deleteHistoryInfor(int index){
          System.out.println("删除的index:"+index);
          Vector v;
          v=getHistoryInfor(); 
          v.remove(index);
          try{
          ObjectOutputStream w=new ObjectOutputStream(
             new FileOutputStream("com/herb/server/Infor/History/HistoryInformation.txt")); 
               w.writeObject(v);
          }catch(Exception e){
               System.out.println(e.getMessage());
          }   
       }
 } 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -