項目實戰----基於文件存儲的學生信息管理(IO)

學習IO後的一個小實驗項目:

項目要求:

1.錄入學生的個人信息:姓名;年齡;性別;學號;

2.對信息進行:增加、刪除、查看、更新操作

3.存儲方式爲:student.txt

 

Student類:

public class Student {
	private String id;
	private String name;
	private int age;
	private String sex;
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student( String id,String name,int age ,String sex) {
		this.age=age;
		this.id=id;
		this.name=name;
		this.sex=sex;
		// TODO Auto-generated constructor stub
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

 StuOption類:封裝了所有用於操作的方法

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

public class StuOption {
	public  void instert() {
		/**
		 * 
		 * 插入學生信息的方法
		 */
		Scanner sc=new Scanner(System.in);
		System.out.println("請輸入姓名:");
		String name=sc.next();
		System.out.println("請輸入性別:");
		String sex=sc.next();
		System.out.println("輸入錯誤,請重新輸入");
		sex=sc.next();
		System.out.println("請輸入學號:");
		String id=sc.next();
		System.out.println("請輸入年齡:");
		int age=sc.nextInt();
		Student st=new Student(id,name,age,sex);
		fileRw(st);
	}
	
	public void updat(List<Student> li) {
		/**
		 *將list中的對象寫回到新的文件夾中
		 */

	}
	
	public  List<Student>    readAllStu() {
		/**
		 * 讀取文件裏的所有數據,並將它以對象的形式存儲在集合裏
		 */
		List <Student> l_s= new ArrayList<Student >();
		BufferedReader br=null;
		try {
			 br=new BufferedReader(new FileReader("student.txt")) ;
			 try {
				String sr=br.readLine();
				while(sr!=null){
					StringTokenizer st=new StringTokenizer(sr);
					String sid=st.nextToken();
					String sname=st.nextToken();
					int sage=Integer.parseInt(st.nextToken());
					String ssex=st.nextToken();
					Student stu=new Student(sid,sname,sage,ssex);
					l_s.add(stu);
					sr=br.readLine();
				}
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("讀取失敗");
				}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}finally {
			try {
				if(br!=null){
					br.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("關閉失敗");
			}
		}
		return l_s;
	
		
	}
	
	public void fileRw(Student s){
		/**
		 * 將錄入的信息寫入文件中
		 * 錄入的格式爲:____  ____  _____  _____
		 */
		File fi=new File("student.txt");
		BufferedWriter bw = null;
		try {
			bw=new BufferedWriter(new FileWriter(fi,true));
			String str=s.getId()+" "+s.getName()+" "+s.getAge()+" "+s.getSex()+"\r\n";
			bw.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(bw!=null){
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("信息錄入失敗");
				}
			}
		}		
	}
	public  Student  findById( String id,List<Student> allStudent) {
		/**
		 * 將根據id查找學生對象的過程寫到fidById這個函數裏
		 */
		Student findsu=null;
		for (Student stu : allStudent) {
			if(stu.getId().equals(id)){
				findsu=stu;
				break;
			}
		}
		if(findsu!=null){

			System.out.println("姓名:"+findsu.getName()+"   年齡:"+findsu.getAge()+"   性別:"+findsu.getSex());
		}else{
			System.out.println("編號不存在");
		}
		return findsu;					
	}

	
	public void list2File(List<Student> li) {
		BufferedWriter rw=null;
		try {
			rw=new BufferedWriter(new FileWriter("student.txt")) ;
			for (Student s : li) {
				String str=s.getId()+" "+s.getName()+" "+s.getAge()+" "+s.getSex()+"\r\n";
				try {
					rw.write(str);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("寫入失敗");
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("文件未找到");
		}finally {
			try {
				if(rw!=null){
					rw.close();
				}				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public  void updat_s(Student st) {
		/**
		 * 用於數據的更新
		 * 
		 */
		Scanner sc=new Scanner(System.in);
		System.out.println("請輸入姓名:");
		String name=sc.next();
		System.out.println("請輸入性別:");
		String sex=sc.next();
		System.out.println("請輸入年齡:");
		int age=sc.nextInt();
		String sid=st.getId();
        st.setName(name);st.setSex(sex);st.setAge(age);
     	
	}
	
	/**
	 * 主菜單方法:
	 * 1.展示主菜單選項
	 * 2.輸入選項
	 * 3.分支判斷結構
	 * 4.函數調用
	 */
	
	
	public void showMain() {
		// TODO Auto-generated method stub
		System.out.println("***************************");
		System.out.println("*********1.添加*************");
		System.out.println("*********2.修改*************");
		System.out.println("*********3.刪除*************");
		System.out.println("*********4.查看*************");
		System.out.println("*********5.退出*************");
		System.out.println("請選擇要進行的操作:");
		Scanner sc=new Scanner(System.in);
		int  choice=sc.nextInt();
		StuOption so=null;
		switch(choice){
			case 1:
				System.out.println("添加");
				so=new StuOption();
				so.instert();
				break;
			case 2:
				/**
				 * 1.首先輸入一個id
				 * 2.分行讀取,將對象信息保存到對象中去
				 * 3.將對象保存到      List   readAllStu()方法將文件的內容以對象的形式封裝到list中
				 * 4.循環遍歷List如果相同則返回信息,不同則返回編號
				 */
				System.out.println("請輸入要更新的學生的id:");
				String s4=sc.next();
				so= new StuOption();
				List<Student>allLis4=so.readAllStu();
				Student s2=so.findById(s4, allLis4);
				System.out.println("請按要求輸入要修改的信息");
				so.updat_s(s2);
				so.list2File(allLis4);
				System.out.println("修改成功");
				break;
			case 3:
				
				/**
				 * 0.請輸入需要刪除的人的姓名
				 * 1.根據查找方法,找到待刪除的數據;
				 * 3.將其進行修改;
				 * 4.將其寫入list對象中
				 */
				System.out.println("請輸入要刪除的學生的id:");
				String s1=sc.next();
				so= new StuOption();
				List<Student>allLis2=so.readAllStu();
				Student s3=so.findById(s1, allLis2);
				allLis2.remove(s3);
				so.list2File(allLis2);
				System.out.println("刪除成功");
				break;
			case 4:
				/**
				 * 1.首先輸入一個id
				 * 2.分行讀取,將對象信息保存到對象中去
				 * 3.將對象保存到      List   readAllStu()方法將文件的內容以對象的形式封裝到list中
				 * 4.循環遍歷List如果相同則返回信息,不同則返回編號
				 */
				//輸入:
				System.out.println("請輸入要查詢的學生的id");
				String s=sc.next();
				//讀取
				so= new StuOption();
				List<Student>allLis1=so.readAllStu();
				so.findById(s, allLis1);
				break;
			case 0:
				System.exit(0);
				break;
			default:
				System.out.println("輸入有誤請重新輸入");
		}
	}
	
}

主函數: 

public class StuMain {
	
	public static void main(String[] args) {
		//StuMain sm=new StuMain();
		StuOption st=new StuOption();
		while(true) {
			st.showMain();
		}
	}
}

本次實驗主要爲了聯繫字符流以及文件保存信息的操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章