代码很简单,不做任何解释

package com.dsr.phone;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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() throws IOException {
		// TODO Auto-generated method stub
		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() throws IOException {
		// TODO Auto-generated method stub
		// 方法一
		 //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 {

	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;
	}
}

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐