(Java) single person information management program

To expand the previous menu program, it is required to add a person's complete information, including name and age. You ...
1, Add file operation class
2, Person class
3, Operation class

To expand the previous menu program, it is required to add a person's complete information, including name and age. You can also modify, delete and query this information after saving.
You can use object serialization to save. You need to add a file operation class, which is specially responsible for saving and reading the contents of the file, and modify the operation class to add specific operations:

Article directory

1, Add file operation class

package Chapter_9; import java.io.*; //This class is dedicated to saving and reading objects public class FileOperate { private File file = null;//Define a file object public FileOperate(String pathName){//Passing the file path through the construction method this.file = new File(pathName);//Instantiate File object } public boolean save(Object obj) throws Exception{//Objects can be saved ObjectOutputStream oos = null;//Object output flow boolean flag = false;//Define operation flag bit try{ oos = new ObjectOutputStream(new FileOutputStream(this.file));//Instanced object output flow oos.writeObject(obj);//Save objects flag = true; }catch (Exception e){ throw e;//Exception thrown }finally { if (oos != null){//Determine whether the object output flow object is instantiated oos.close();//Shut down whether there is an exception or not } } return flag; } public Object load()throws Exception{//read object Object obj = null;//Receive saved objects ObjectInputStream ois = null;//Declare object input flow try{ ois = new ObjectInputStream(new FileInputStream(this.file));//Instanced object input flow obj = ois.readObject();//read object }catch (Exception e){ throw e; }finally { if (ois !=null){//Determine whether the input stream is instantiated ois.close();//Close input stream } } return obj; } }

The function of a class in a program is to write and read objects to the program. Only one path needs to be passed in for operation

2, Person class

import java.io.*; public class Person implements Serializable {//Objects of this class can be serialized private String name; private int age; public Person(String name,int age){//Setting property content through construction method this.name = name; this.age = age; } public String toString(){ return "Full name:" + this.name + "; Age:" + this.age; } 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; } }

3, Operation class

package Chapter_9; public class Operate { public static void add(){//Add data operation InputData input = new InputData();//Instantiate input data object FileOperate fo = new FileOperate("D://test.txt"); String name = input.getString("Please enter your name:"); int age = input.getInt("Please enter age:","Age must be a number!"); Person per = new Person(name,age);//Instantiate Person object try { fo.save(per);//Save objects }catch (Exception e){ e.printStackTrace(); } System.out.println("Information added successfully"); } public static void delete(){//Delete data operation FileOperate fo = new FileOperate("D://test.txt"); try{ fo.save(null);//Clear objects }catch (Exception e){ e.printStackTrace(); } System.out.println("Information deleted successfully!"); } public static void update(){//Modify data operation InputData input = new InputData();//Instantiate input data object FileOperate fo = new FileOperate("D:\\test.txt"); Person per = null; try{ per = (Person)fo.load();//Read data }catch (Exception e1){ e1.printStackTrace(); } String name = input.getString("Please enter a new name (original name:" + per.getName() + "):"); int age = input.getInt("Please enter a new age (old age:" + per.getAge() + ")","Age must be a number!"); per = new Person(name,age);//Re instancing objects try{ fo.save(per);//Resave objects }catch (Exception e){ e.printStackTrace(); } System.out.println("Information updated successfully"); } public static void find(){ FileOperate fo = new FileOperate("D:\\test.txt"); Person per = null; try{ per = (Person)fo.load();//read object }catch (Exception e1){ e1.printStackTrace(); } System.out.println(per); } }
South Huai Bei'an 561 original articles published, 260 praised, 220000 visitors+ His message board follow

12 February 2020, 09:54 | Views: 9805

Add new comment

For adding a comment, please log in
or create account

0 comments