〖JAVA IO〗_執行個體操作—單人資訊管理程式筆記
執行個體要求:
將之前的菜單程式進行擴充,要求:增加的時候可以增加一個人的完整資訊,人的資訊包括姓名和年齡。儲存之後也可以修改此資訊、刪除此資訊,查詢此資訊此代碼該如何完成?
提示:使用對象序列化儲存。
此時程式可以使用之前講解過的幾個類:InputData、Person、Operate、Menu。需要增加檔案操作類,專門負責儲存和讀取檔案的內容,以及修改Operate類,為其增加具體的操作。
檔案操作類:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class FileOperate{ private File file = null; public FileOperate(String pathName){ this.file = new File(pathName); } public boolean save(Object obj) throws Exception{ ObjectOutputStream oos = null; boolean flag = false;//定義操作標誌位 try{ oos = new ObjectOutputStream(new FileOutputStream(this.file)); oos.writeObject(obj); flag = true; }catch(Exception e){ throw e;// 有異常交給被調用處處理 }finally{ if(oos!=null){ oos.close(); } } return flag; } public Object load()throws Exception{ Object obj = null; ObjectInputStream ois = null; try{ ois = new ObjectInputStream(new FileInputStream(this.file)); obj = ois.readObject(); //讀取對象 }catch(Exception e){ throw e; }finally{ if(ois!=null){ ois.close();//關閉 } } return obj; }}
操作是的人員資訊,所以一定要定義Person類。
import java.io.Serializable;public class Person implements Serializable{ private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";年齡:"+this.age; } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public String getName(){ return this.name; } public int ageAge(){ return this.age; }}
修改操作類:
public class Operate{ public static void add(){ InputData input = new InputData(); FileOperate fo = new FileOperate("d:\\test.per"); String name = input.getString("請輸入姓名:"); int age = input.getInt("請輸入年齡:","年齡必須是數字!"); Person per = new Person(name,age); //執行個體化Person對象 try{ fo.save(per); }catch(Exception e){ e.printStackTrace(); } System.out.println("資訊增加成功!"); } public static void delete(){ FileOperate fo = new FileOperate("d:\\test.per"); try{ fo.save(null); //儲存對象 }catch(Exception e){ e.printStackTrace(); } System.out.println("資訊刪除成功!"); } public static void update(){ InputData input = new InputData(); FileOperate fo = new FileOperate("d:\\test.per"); Person per = null; try{ per = (Person)fo.load(); //讀取對象 }catch(Exception e){ e.printStackTrace(); } String name = input.getString("請輸入姓名(原姓名:"+per.getName()+")"); int age = input.getInt("請輸入年齡(原年齡:"+per.getAge()+")","年齡必須是數字"); per = new Person(name,age); //執行個體化Person對象 try{ fo.save(per); }catch(Exception e){ e.printStackTrace(); } System.out.println("資訊修改成功!"); } public static void find(){ FileOperate fo = new FileOperate("d:\\test.per"); Person per = null; try{ per = (Person)fo.load(); }catch(Exception e){ e.printStackTrace(); } System.out.println(per); }}
編寫InputData類
import java.io.*;import java.util.*;import java.text.*;public class InputData{ private BufferedReader buf = null; public InputData(){ this.buf = new BufferedReader(new InputStreamReader(System.in)); } public String getString(String info){ String temp = null; System.out.print(info); //列印提示資訊 try{ temp = this.buf.readLine(); }catch(IOException e){ e.printStackTrace(); } return temp; } public int getInt(String info,String err){ int temp = 0; String str = null; boolean flag = true; while(flag){ str = this.getString(info); if(str.matches("^\\d+$")){//判斷是否由數字組成 temp = Integer.parseInt(str); flag = false; }else{ System.out.println(err); } } return temp; } public float getFloat(String info,String err){ float temp=0; String str = null; boolean flag = true; while(flag){ str = this.getString(info); if(str.matches("^\\d+.?\\d+$")){ temp = Float.parseFloat(str); flag = false; }else{ System.out.println(err); } } return temp; } public Date getDate(String info,String err){ Date temp = null; String str = null; boolean flag = true; while(flag){ str = this.getString(info); if(str.matches("^\\d{4}-\\d{2}-\\d{2}$")){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try{ temp = sdf.parse(str); }catch(Exception e){} flag = false; }else{ System.out.println(err); } } return temp; }}
測試InputData類
import java.io.* ;import java.util.* ;public class TestInput{ public static void main(String args[]) throws Exception{ InputData input = new InputData() ; // float f = input.getFloat("請輸入小數:","輸入的不是小數,請重新輸入!") ; // System.out.println("小數:" + f) ; Date d = input.getDate("請輸入日期,格式為(yyyy-mm-dd):","輸入的日期格式不正確,請重新輸入") ; System.out.println("日期" + d) ; }};
編寫Menu菜單類
public class Menu{ public Menu(){ while(true){ this.show() ; // 無限制調用菜單的顯示 } } public void show(){ System.out.println("===== Xxx系統 =====") ; System.out.println(" [1]、增加資料") ; System.out.println(" [2]、刪除資料") ; System.out.println(" [3]、修改資料") ; System.out.println(" [4]、查看資料") ; System.out.println(" [0]、系統退出\n") ; InputData input = new InputData() ; int i = input.getInt("請選擇:","請輸入正確的選項!") ; switch(i){ case 1:{ Operate.add() ; // 調用增加操作 break ; } case 2:{ Operate.delete() ; // 調用刪除操作 break ; } case 3:{ Operate.update() ; // 調用更新操作 break ; } case 4:{ Operate.find() ; // 調用查看操作 break ; } case 0:{ System.exit(1) ; // 系統退出 break ; } default:{ System.out.println("請選擇正確的操作!") ; } } }};
用戶端類
import java.io.* ;public class ExecDemo03{ public static void main(String args[]) throws Exception{ new Menu() ; }};
操作成功: