java 热腾腾的代码

  1 package TaskDemos;
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 import java.io.Serializable;
8 import java.util.Scanner;
9
10
11 class Operate {
12 public static void add(){
13 FileOperate fo=new FileOperate("d:\\test.txt");
14 System.out.println("====添加学生====");
15 Scanner sc=new Scanner(System.in);
16 System.out.print("请输入姓名:");
17 String name=sc.nextLine();
18 System.out.print("请输入性别:");
19 String sex=sc.nextLine();
20 System.out.print("请输入出生日期:");
21 String birth=sc.nextLine();
22 Person per=new Person(name,sex,birth);
23 try{
24 fo.save(per);
25 }catch (Exception e){
26 e.printStackTrace();
27 }
28 }
29
30 public static void find(){
31 FileOperate fo=new FileOperate("d:\\test.txt");//存放路径
32 Person per=null;
33 System.out.println("====查看学生====");
34 Scanner sc=new Scanner(System.in);
35 System.out.print("请输入姓名:");
36 String name=sc.nextLine();
37 try{
38 per=(Person)fo.load();
39 if(per.getName().equals(name)){
40 System.out.print(per.toString());
41 }else{
42 System.out.printf("%s不存在\n",name);
43 }
44 }catch (Exception e){
45 e.printStackTrace();
46 }
47 }
48 }
49 class Menu {
50 public Menu(){
51 while(true){
52 this.show();
53 }
54 }
55 public void show(){
56 System.out.println("======学生信息管理=====");
57 System.out.println("1.添加学生");
58 System.out.println("2.查看学生");
59 System.out.println("3.退出");
60 System.out.println("==按数字键选择==");
61 Scanner sc=new Scanner(System.in);
62 int i=sc.nextInt();
63 switch(i){
64 case 1:{
65 Operate.add();
66 break;
67 }
68 case 2:{
69 Operate.find();
70 break;
71 }
72 case 3:{
73 System.exit(1);
74 break;
75 }
76 default:{
77 System.out.println("请选择正确的操作!");
78 }
79 }
80 }
81 }
82 class FileOperate {
83 private File file=null;
84 public FileOperate(String pathName){
85 this.file=new File(pathName);
86 }
87 public boolean save(Object obj) throws Exception{
88 ObjectOutputStream oos=null;
89 boolean flag=false;
90 try{
91 oos=new ObjectOutputStream(new FileOutputStream(this.file));
92 oos.writeObject(obj);
93 flag=true;
94 }catch (Exception e){
95 throw e;
96 }finally{
97 if(oos!=null){
98 oos.close();
99 }
100 }
101 return flag;
102 }
103 public Object load() throws Exception{
104 Object obj=null;
105 ObjectInputStream ois=null;
106 try{
107 ois=new ObjectInputStream(new FileInputStream(this.file));
108 obj=ois.readObject();
109 }catch (Exception e){
110 throw e;
111 }finally{
112 if(ois!=null){
113 ois.close();
114 }
115 }
116 return obj;
117 }
118 }
119 class Person implements Serializable{
120
121 private static final long serialVersionUID = 1L;
122 private String name;
123 private String sex;
124 private String birthday;
125 public Person(String name,String sex,String birthday){
126 this.name=name;
127 this.sex=sex;
128 this.birthday=birthday;
129 }
130 public String getBirthday() {
131 return birthday;
132 }
133 public void setBirthday(String birthday) {
134 this.birthday = birthday;
135 }
136 public String getName() {
137 return name;
138 }
139 public void setName(String name) {
140 this.name = name;
141 }
142 public String getsex() {
143 return sex;
144 }
145 public void setsex(String sex) {
146 this.sex = sex;
147 }
148 public String toString(){
149 return "姓名:"+this.name+"\n"+"性别:"+this.sex+"\n"+"出生:"+this.birthday+"\n";
150 }
151
152 }
153 public class A13_M3 {
154 public static void main(String[] args) {
155 new Menu();
156 }
157 }
原文地址:https://www.cnblogs.com/dennisac/p/2424208.html