Java实例---简单的个人管理系统
代码分析
FileOperate.java
1 package com.ftl.testperson; 2 import java.io.File ; 3 import java.io.FileInputStream ; 4 import java.io.FileOutputStream ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 public class FileOperate{ // 此类专门用于保存和读取 8 private File file = null ; // 定义文件对象 9 public FileOperate(String pathName){ // 通过 构造传递文件路径 10 this.file = new File(pathName) ; 11 } 12 public boolean save(Object obj) throws Exception{ // 保存对象 13 ObjectOutputStream oos = null ; // 对象输出流 14 boolean flag = false ; // 定义操作标志位 15 try{ 16 oos = new ObjectOutputStream(new FileOutputStream(this.file)) ; // 实例化对象输出流 17 oos.writeObject(obj) ; // 写入对象 18 flag = true ; 19 }catch(Exception e){ 20 throw e ; // 有异常交给被调用处处理 21 }finally{ 22 if(oos!=null){ 23 oos.close() ; 24 } 25 } 26 return flag ; 27 } 28 public Object load() throws Exception{ // 读取对象 29 Object obj = null ; // 接收读取的内容 30 ObjectInputStream ois = null ; 31 try{ 32 ois = new ObjectInputStream(new FileInputStream(this.file)) ; // 实例化对象输入流 33 obj = ois.readObject() ; // 读取对象 34 }catch(Exception e){ 35 throw e ; 36 }finally{ 37 if(ois!=null){ 38 ois.close() ; // 关闭 39 } 40 } 41 return obj ; 42 } 43 };
InputData.java
1 package com.ftl.testperson; 2 3 import java.io.* ; 4 import java.util.* ; 5 import java.text.* ; 6 public class InputData{ 7 private BufferedReader buf = null ; 8 public InputData(){// 只要输入数据就要使用此语句 9 this.buf = new BufferedReader(new InputStreamReader(System.in)) ; 10 } 11 public String getString(String info){ // 得到字符串信息 12 String temp = null ; 13 System.out.print(info) ; // 打印提示信息 14 try{ 15 temp = this.buf.readLine() ; // 接收数据 16 }catch(IOException e){ 17 e.printStackTrace() ; 18 } 19 return temp ; 20 } 21 public int getInt(String info,String err){ // 得到一个整数的输入数据 22 int temp = 0 ; 23 String str = null ; 24 boolean flag = true ; // 定义一个标记位 25 while(flag){ 26 str = this.getString(info) ; // 接收数据 27 if(str.matches("^\d+$")){ // 判断是否由数字组成 28 temp = Integer.parseInt(str) ; // 转型 29 flag = false ; // 结束循环 30 }else{ 31 System.out.println(err) ; // 打印错误信息 32 } 33 } 34 return temp ; 35 } 36 public float getFloat(String info,String err){ // 得到一个小数的输入数据 37 float temp = 0 ; 38 String str = null ; 39 boolean flag = true ; // 定义一个标记位 40 while(flag){ 41 str = this.getString(info) ; // 接收数据 42 if(str.matches("^\d+.?\d+$")){ // 判断是否由数字组成 43 temp = Float.parseFloat(str) ; // 转型 44 flag = false ; // 结束循环 45 }else{ 46 System.out.println(err) ; // 打印错误信息 47 } 48 } 49 return temp ; 50 } 51 public Date getDate(String info,String err){ // 得到一个小数的输入数据 52 Date temp = null ; 53 String str = null ; 54 boolean flag = true ; // 定义一个标记位 55 while(flag){ 56 str = this.getString(info) ; // 接收数据 57 if(str.matches("^\d{4}-\d{2}-\d{2}$")){ // 判断是否由数字组成 58 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ; 59 try{ 60 temp = sdf.parse(str) ; // 将字符串变为Date型数据 61 }catch(Exception e){} 62 flag = false ; // 结束循环 63 }else{ 64 System.out.println(err) ; // 打印错误信息 65 } 66 } 67 return temp ; 68 } 69 };