1. 程式人生 > >簡易學生管理系統(IO流、檔案、)

簡易學生管理系統(IO流、檔案、)

之前用ArrayList做過簡易管理學生管理系統。實現原理基本差不多。從鍵盤輸入到程式,讀出來賦給兩個變數(姓名和學號),寫物件(Object物件流)到檔案........

具體實現如下:

package cn.SystemStudent;

import java.io.File;
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.io.Serializable;




public class Student implements Serializable{
	String name;
    int number;
	
	/**與構造方法有相同作用
    public Student(String name,int number){
    	this.name=name;
    	this.number=number;
    }*/
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	//static File dir = new File("f:/StudentTest/");//檔案目錄
	private static final String STUDENT_STORE_DIR = "E://student_store_dir/";
	/**
	 * 新增學生
	 * 原理:呼叫鍵盤輸入方法,把學生屬性寫程序序,再從程式把屬性輸出到檔案
	 */
	public static void addStudent(){
		
		Student student = new Student();//例項化一個學生物件,
		System.out.println("請輸入學生姓名:");
		student.name=FileStudentUtil.getStringKeyBoard();
		System.out.println("您輸入的學生姓名是:"+student.name);
		
		System.out.println("請輸入學生學號:");
		student.number=FileStudentUtil.getintKeyBoard();
		System.out.println("您輸入的學生學號是:"+student.number);
		
		File file= new File(STUDENT_STORE_DIR,student.name);//目錄下的檔名(學生姓名)
		FileOutputStream fos = null;
		ObjectOutputStream osr = null;
		try {
			fos = new FileOutputStream(file);
			osr = new ObjectOutputStream(fos);
			osr.writeObject(student);//把物件寫進檔案
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(osr != null){
				try {
					osr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 得到單個學生物件
	 * 實現原理:把物件從檔案中輸入到程式中(從流中讀出物件,賦給另一個物件,返回)
	 * 
	 */
	public static Student getStudent(String name){
		File file = new File(STUDENT_STORE_DIR,name);
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			Student student= (Student) ois.readObject();
			return student;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(ois != null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	 
	/**
	 * 輸出所有學生:
	 * 實現原理:通過dir.listFiles();檔案目錄下檔案,給陣列接收。遍歷檔案,呼叫單個檔案方法
	 * java.io.InvalidClassException:此錯誤可以把檔案內容刪除重新寫入後正常,我想應該可以重新整理解決吧
	 */
	public static void showAllStudent(){
		System.err.println("姓名\t學號");
		File dir = new File(STUDENT_STORE_DIR);//new一個檔案,把路徑傳進來。才可以呼叫dir.listFiles()方法
		File[] file = dir.listFiles();//dir.listFiles();目錄下的所有檔案,給陣列接收
//		File[] file = dir.listFiles();new檔案物件,路徑傳參時,直接用此行代替上面兩行程式碼
		for(File f : file){//遍歷檔案,
			
			//呼叫單個檔案方法,用檔名傳參,上面方法讀出學生物件。最後賦給新學生物件,列印輸出
			Student stu = getStudent(f.getName());
			System.out.println(stu);
		}
	}
	/**
	 *重寫to String
	 */
	@Override
	public String toString() {
		String s=name+"\t"+number;
		return s;
	}
	/**
	 * 通過學生學號找到對應學生
	 */
	public static Student findStudent(int number){
		File dir = new File(STUDENT_STORE_DIR);
		File[] file = dir.listFiles();
		for(File f : file){
			Student student = getStudent(f.getName());
//			if(student.number!=number){
//				return null;
//			}
			if(student.number==number){
				return student;
			}
		}
		return null;
	}
	/**
	 * 通過學生學號找到對應學生,再通過學生姓名找到檔名刪除
	 * 除系統標準輸入輸出的流,其他流要關閉,不然刪除不掉。因為會當成正在使用。
	 */
	public static void deleteStudent2(){
		System.out.println("請輸入刪除學生學號:");
		int number = FileStudentUtil.getintKeyBoard();
		Student stu =findStudent(number);
		File file = new File(STUDENT_STORE_DIR,stu.name);
		System.out.println(file.getPath());
		
		System.out.println((file.delete())?"刪除學生"+stu.name+"成功":"刪除失敗");
		
	}
	
	public static void findStduentBynumber(){
		System.out.println("請輸入查詢的學生學號:");
		int num = FileStudentUtil.getintKeyBoard();
		File dir = new File(STUDENT_STORE_DIR);
		File[] file = dir.listFiles();
		for(File f : file){
			Student student = getStudent(f.getName());
			if(student.number!=num){
				System.out.println("此學號學生不存在!");
			}
			if(student.number==num){
				System.out.println("你要查詢的學生是:"+student.name);
			}
		}
		
	}
	/**
	public static void deleteStudent(){
		System.out.println("請輸入需要刪除學生的學號:");
		int number=FileStudentUtil.getintKeyBoard();
		
		boolean flag = deleteStudent(number);
		
		System.out.println(flag?"刪除成功":"刪除失敗");
	}
	
	public static boolean deleteStudent(int number){
		File[] file = dir.listFiles();
		for(File f : file){
			Student student = getStudent(f.getName());
			if(student.number==number){
				
				if(!f.delete()){
					File dF = new File(dir,student.name);
					return dF.delete();
				}else{
					return true;
				}
			}
		}
		return false;
	}*/
}

package cn.SystemStudent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileStudentUtil {
	/**
	 * 使用io流,獲取從鍵盤輸入到程式。使用Input(從外部裝置到程式為輸入,程式到外部裝置而為輸出);
	 * System.in(out)標準系統輸入輸出中,不需要關閉流。否則在後面測試類中會回報流關閉錯誤
	 */
	public static String getStringKeyBoard(){
		String name="";
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(System.in);
			br = new BufferedReader(isr);
			name = br.readLine();
//			int number=Integer.parseInt(name);把String型別轉換成int型
		} catch (IOException e) {
			e.printStackTrace();
		}
//		finally{
//				try {
//					if(br!=null)
//					br.close();
//					if(isr!=null)
//						isr.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//		}
		return name;
	}
	
	public static int getintKeyBoard(){
		while(true){
		int number=0;
		InputStreamReader isr= null;
		BufferedReader br = null;
		try {
			isr= new InputStreamReader(System.in);
			br = new BufferedReader(isr);
			String s=br.readLine();//在緩衝流中用readLine(),每行讀成String型。在轉換成int型
			number=Integer.parseInt(s);
			
			return number;
			
		} catch (IOException e) {
			e.printStackTrace();	
			}catch(NumberFormatException a){
				System.out.println("請輸入正確數值格式");
			}
		}
		//return 0;
	}
}
package cn.SystemStudent;

import java.util.Scanner;



/**
 * 專案名稱: 《學生資訊管理系統》 英文:StudentManagerSystemFile
版本號:  V1.1
功能描術: 學生資訊管理系統具有能對學生資訊進行顯示,新增,刪除,查詢(根據學號查詢),修改,退出功能.

要求:1>在學生資訊管理系統1.0版本基礎上對學生資訊實現持久儲存.

              建立檔案操作工具類FileUtil.
          封裝學生持久儲存功能:
             新增學生(addStudent)
             顯示學生列表(showAllStudent)
             刪除學生(deleteStudent)
             查詢學生(findStudentByNumber)
             建立檔案方法 File createFile(String path) 
      .......
      
          定義:儲存路徑常量
        private static final String STUDENT_STORE_DIR = "E://student_store_dir/";
	
	2> 將v1.0版本中通過Scanner類獲取鍵盤輸入資料 更改為 從字元緩衝流讀取資料
 */
public class Test {
	
	public static final int ADDSTUDENT=1;
	
	public static void main(String[] args) {
		
		System.out.println("=====歡迎進入學生管理系統=====");
		System.out.println(" 1 新增學生,2 顯示學生列表,3 刪除學生,4 查詢學生,5 退出");
		System.out.println("=====新增學生資訊操作====="); 
		System.out.println("<請輸入操作命令:>");
		
		for(;;){
			int number=FileStudentUtil.getintKeyBoard();
			switch(number){
				case ADDSTUDENT:
					Student.addStudent();
					
					break;
				case 2:
					Student.showAllStudent();
					
					break;
				case 3:
					Student.deleteStudent2();
					
					break;
				case 4:
					Student.findStduentBynumber();
					
					break;
				case 5:
					System.out.println("退出系統成功:");
					System.exit(0);
			}
			System.out.println("<請輸入操作命令:>");
		}
//		String s=FileStudentUtil.getStringKeyBoard();
//		System.out.println(s);
		//Student.addStudent();
		//Student.getStudent(name);
//		Student.showAllStudent();
		//Student.deleteStudent2();
//		Student.findStduentBynumber();
	}
}