您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
〖第12章:JAVA IO〗_实例操作—单人信息管理程序
发布时间:2020-12-31 16:52:25编辑:雪饮阅读()
基于对象序列化完成一个单人信息管理程序
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.io.Serializable;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;
import java.io.EOFException;
class Menu{
private Operate operate=new Operate();
public Menu(){
this.show();
}
public void show(){
System.out.println("[0]初始化");
System.out.println("[1]增加数据");
System.out.println("[2]删除数据");
System.out.println("[3]修改数据");
System.out.println("[4]查看数据");
System.out.println("[5]退出系统");
try{
int select_index=new InputData().getInt("请选择操作:","只能输入数字!");
if(select_index==0){
operate.init();
}
if(select_index==1){
operate.add();
}
if(select_index==2){
operate.delete();
}
if(select_index==3){
operate.update();
}
if(select_index==4){
operate.find();
}
if(select_index==5){
System.exit(0);
}
}
catch(NullPointerException e){
//监听用户ctrl+c
System.exit(0);
}
catch(Exception e){
e.printStackTrace() ;
}
}
}
class Person implements Serializable{
private static final long serialVersionUID=1L;
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;
}
}
class Operate{
private File file=new File("d:"+File.separator+"kasumis.txt");
public void init(){
try {
if(!this.file.exists())
{
this.file.createNewFile();
}
else{
FileWriter fileWriter =new FileWriter(this.file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
}
System.out.println("初始化完成");
new Menu();
}
catch(IOException e){
e.printStackTrace();
}
}
public Object[] getSerializeables(){
Object obj[]=null;
try{
InputStream input = new FileInputStream(this.file) ;
ObjectInputStream ois = new ObjectInputStream(input) ;
obj = (Object[])ois.readObject() ;
ois.close() ;
}
catch(EOFException e){
//空文件没有任何序列化对象时会报该异常EOFException
obj=new Object[0];
}
catch(FileNotFoundException e){
e.printStackTrace() ;
}
catch(IOException e){
e.printStackTrace() ;
}
catch(ClassNotFoundException e){
e.printStackTrace() ;
}
return obj;
}
public void ser(Object obj[]) throws Exception {
OutputStream out = new FileOutputStream(this.file) ;
ObjectOutputStream oos = new ObjectOutputStream(out) ;
oos.writeObject(obj) ;
oos.close() ;
}
public void add() throws Exception{
String name=new InputData().getString("请输入姓名:");
int age=new InputData().getInt("请输入年龄:","只能输入数字!");
Object oldData[]=getSerializeables();
int slen=oldData.length;
Person pers[]= new Person[slen+1];
for(int i=0;i<slen;i++){
pers[i]=(Person)oldData[i];
}
pers[slen]=new Person(name,age);
this.ser(pers);
System.out.println("增加成功");
new Menu();
}
public void delete() throws Exception{
Object obj[]=getSerializeables();
if(obj.length==0){
System.out.println("暂无任何数据!");
}
else{
for(int i=0;i<obj.length;i++){
System.out.println("["+i+"]:"+obj[i]);
}
int select_index=new InputData().getInt("请输入要删除的数据下标:","只能输入数字!");
Object newObj[]=new Object[obj.length-1];
int temp=0;
for(int i=0;i<obj.length;i++){
if(i!=select_index){
newObj[temp]=obj[i];
}
temp++;
}
this.ser(newObj);
System.out.println("删除成功");
}
new Menu();
}
public void update() throws Exception{
Object obj[]=getSerializeables();
if(obj.length==0){
System.out.println("暂无任何数据!");
}
else{
for(int i=0;i<obj.length;i++){
System.out.println("["+i+"]:"+obj[i]);
}
int select_index=new InputData().getInt("请输入要修改的数据下标:","只能输入数字!");
String name=new InputData().getString("请输入姓名:");
int age=new InputData().getInt("请输入年龄:","只能输入数字!");
Object person=new Person(name,age);
for(int i=0;i<obj.length;i++){
if(i==select_index){
obj[i]=person;
}
}
this.ser(obj);
System.out.println("修改成功");
}
new Menu();
}
public void find(){
Object obj[]=getSerializeables();
if(obj.length==0){
System.out.println("暂无任何数据!");
}
else{
for(int i=0;i<obj.length;i++){
System.out.println("["+i+"]:"+obj[i]);
}
}
String q=new InputData().getString("输入‘q’键回首页:");
if(q.equals("q")){
new Menu();
}
}
};
class InputData{
private BufferedReader 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 class Hello{
public static void main(String args[]){
new Menu();
}
};
![单人信息管理程序](/d/file/xuewuzhijing/xindebiji/f8ecb040c19967237c8d6cb2de81fd61.png)
程序还有几处性能问题需要优化。
关键字词:java,io,单人信息管理程序