java控制台电话本 用io流和序列化实现

代码很简单,不做任何解释了,不明白找我


package com.dsr.phone;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class phone {
	List<books> listbook = new ArrayList<books>();
	Scanner sc = new Scanner(System.in);
	boolean startsingle = true;
	private String name;
	private String num;
	static File f = null;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		phone p = new phone();
		f = new File("d:/phone.txt");
		if (!f.exists()) {
			f.createNewFile();

		} else {
			p.readbook();
		}
		p.run();
	}

	private void run() throws IOException {

		// TODO Auto-generated method stub
		while (startsingle) {

			System.out.println("———————————————————电话本———————————————————");
			System.out.println("1.添加\t2.删除\t3.修改\t4.查询\t5.打印全部\t6.保存\t0.退出");
			System.out.println("———————————————————电话本———————————————————");
			System.out.println("请输入数字选择功能");
			String select = sc.next();
			switch (select) {
			case "1":
				add();
				System.out.println("添加成功");
				break;
			case "2":
				delete();
				System.out.println("删除成功");
				break;
			case "3":
				update();
				System.out.println("修改成功");
				break;
			case "4":
				search();
				break;
			case "5":
				printall();
				break;
			case "6":
				savebook();
				System.out.println("保存成功");
				break;
			case "0":
				sc.close();
				startsingle = false;

				break;

			default:
				System.out.println("输错了,重来!!!!");
				break;
			}
		}
	}

	private void readbook() {
		// TODO Auto-generated method stub
		ObjectInputStream objin = null;
		try {
			objin = new ObjectInputStream(new FileInputStream("d:/phone1.txt"));
			listbook = (List<books>) objin.readObject();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();

		} finally {
			try {
				objin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// FileInputStream fi = new FileInputStream(f);
		// BufferedReader br = new BufferedReader(new InputStreamReader(fi));
		// String line = null;
		// while ((line = br.readLine()) != null) {
		// String[] str = line.split(",");
		// books b = new books(str[0], str[1]);
		// listbook.add(b);
		// b = null;
		// }
		// fi.close();
		// br.close();

		// BufferedReader br = new BufferedReader(new FileReader(f));
		// String line = null;
		// while ((line = br.readLine()) != null) {
		// String[] str = line.split(",");
		// books b = new books(str[0], str[1]);
		// listbook.add(b);
		// b = null;
		// }
		// br.close();

	}

	private void savebook() {
		// TODO Auto-generated method stub
		// 序列化
		ObjectOutputStream obj = null;
		try {
			obj = new ObjectOutputStream(new FileOutputStream("d:/phone1.txt"));
			obj.writeObject(listbook);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("找不到文件");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				obj.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// // 方法一
		// FileOutputStream fo = new FileOutputStream(f);
		// byte[] bytes = new byte[1024];
		// for (books b : listbook) {
		// bytes = b.printsave().getBytes();
		// fo.write(bytes, 0, b.printsave().length());
		// fo.write("\r\n".getBytes());
		// fo.flush();
		// }
		// fo.close();//close
		// 方法二:
		// PrintWriter pw = new PrintWriter(f);
		// for (books b : listbook) {
		// pw.write(b.printsave());
		// pw.write("\r\n");
		// }
		// pw.close();
		// 方法三:
		// FileOutputStream fo = new FileOutputStream(f);
		// for (books b : listbook) {
		// fo.write(b.printsave().getBytes());
		// fo.write("\r\n".getBytes());
		// }
		// fo.close();
		// 方法四:
		// PrintStream pr = new PrintStream(f);
		// for (books b : listbook) {
		// pr.println(b.printsave());
		// }
		// pr.flush();
		// pr.close();

	}

	private void printall() {
		// TODO Auto-generated method stub
		for (books b : listbook) {
			System.out.println(b.printshow());
		}
	}

	private void search() {
		// TODO Auto-generated method stub

		System.out.println("输入姓名");
		String name = sc.next();
		int index = find(name);
		if (index != -1) {
			System.out.println(listbook.get(index).printshow());

		}
	}

	private void update() {
		// TODO Auto-generated method stub
		delete();
		add();
	}

	private void delete() {
		// TODO Auto-generated method stub
		System.out.println("输入姓名");
		String name = sc.next();
		int index = find(name);
		if (index != -1) {
			listbook.remove(index);

		}

	}

	private int find(String name) {
		// TODO Auto-generated method stub
		for (books b : listbook) {
			if (b.getName().equals(name)) {
				return listbook.indexOf(b);
			}
		}
		System.out.println("找不到此人");
		return -1;
	}

	private void add() {
		// TODO Auto-generated method stub
		System.out.println("输入姓名!!!!!");
		name = sc.next();
		System.out.println("输入电话!!!!!");
		num = sc.next();
		books b = new books(name, num);
		listbook.add(b);
		System.out.println(b.printshow());

	}

}

class books implements Serializable {

	private String name;

	private String num;

	books(String name, String num) {
		this.name = name;
		this.num = num;
	}

	public String getName() {
		return name;
	}

	public String getNum() {
		return num;
	}

	public String printshow() {
		return "\t姓名:" + name + "\t电话:" + num;

	}

	public String printsave() {
		return name + "," + num;
	}
}


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值