之前想写一个电话簿,看了网上的很多都不能保存操作记录,现在自己写了一个能够将数据操作记录保存在txt文件中的电话簿小程序,实现了增删改查以及查询全部的功能。代码如下:
package dianhuaben;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
int i=0;
Scanner sc = new Scanner(System.in);
File file=new File("word.txt");
public Main() throws IOException {
xuanZe();
}
public void xuanZe() throws IOException {
System.out.println("----------电话本管理系统----------");
System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 6.退出");
System.out.println("----------电话本管理系统----------\n 请选择操作:");
String a = sc.next();
switch (a) {
case "1":
add();
break;
case "2":
delete();
break;
case "3":
xiuGai();
break;
case "4":
selectAll();
break;
case "5":
selectByName();
break;
case "6":
tuiChu();
break;
default:
System.out.println("输入指令错误!请重新输入!");
xuanZe();
break;
}
}
public void add() throws IOException {
ZiLiao b = new ZiLiao();
System.out.println("------添加电话本------");
System.out.println("请输入相关信息:");
System.out.print("姓名:");
String name = sc.next();
b.setName(name);
System.out.print("电话:");
String dianhua = sc.next();
b.setPhoneNumber(dianhua);
FileWriter writer;
try {
writer = new FileWriter("word.txt",true);
writer.write(name);
writer.write(":");
writer.write(dianhua);
writer.write("\r\n");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("添加成功");
System.out.println(b.toString());
xuanZe();
}
public void delete() throws IOException {
System.out.println("------删除电话本------");
System.out.println("请输入你要删除的电话本:");
String s=sc.next();
try {
BufferedReader br=new BufferedReader(new FileReader(file));
ArrayList<String>list=new ArrayList<>();
String str;
while(true) {
str=br.readLine();
if(str==null) break;
String[] temp=str.split(":");
String na=temp[0];
String dh=temp[1];
int index=str.indexOf(s);
if(index!=-1) {
i=5;
System.out.println("删除成功!");
continue;
}
list.add(str);
}
if(i!=5) {
System.out.println("你输入的账号不存在!请重新输入!");
xiuGai();
}
file.delete();
file.createNewFile();
FileWriter fw=new FileWriter(file);
for(int i=0;i<list.size();i++) {
String str1=(String)list.get(i);
fw.write(str1+"\r\n");
}
br.close();
fw.close();
xuanZe();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void xiuGai() throws IOException {
System.out.println("--------------修改电话本-------------");
System.out.println("请输入你要修改的账号:");
String s = sc.next();
try {
BufferedReader br=new BufferedReader(new FileReader(file));
ArrayList<String>list=new ArrayList<>();
String str;
while(true) {
str=br.readLine();
if(str==null) break;
String[] temp=str.split(":");
String na=temp[0];
String dh=temp[1];
int index=str.indexOf(s);
if(index!=-1) {
System.out.println("你要修改的是:" + str);
System.out.println("请重新输入新号码:");
String s1=sc.next();
str=na+":"+s1;
System.out.print("修改成功!");
System.out.println("修改后为:" + s+":"+s1);
i=5;
}
list.add(str);
}
if(i!=5) {
System.out.println("你输入的账号不存在!请重新输入!");
xiuGai();
}
file.delete();
file.createNewFile();
FileWriter fw=new FileWriter(file);
for(int i=0;i<list.size();i++) {
String str1=(String)list.get(i);
fw.write(str1+"\r\n");
}
br.close();
fw.close();
xuanZe();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void selectAll() throws IOException {
BufferedReader br=new BufferedReader(new FileReader(file));
String line=null;
System.out.println("--------------所有电话信息---------------");
while((line=br.readLine())!=null) {
String[] temp=line.split(":");
String na=temp[0];
String dh=temp[1];
System.out.println("姓名:"+na+" "+" "+"电话:"+dh);}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xuanZe();
}
public void selectByName() throws IOException {
BufferedReader br=new BufferedReader(new FileReader(file));
String line=null;
System.out.print("请输入你要查询的姓名:");
String s=sc.next();
while((line=br.readLine())!=null) {
String[] temp=line.split(":");
String na=temp[0];
String dh=temp[1];
if(s.equals(na)) {
System.out.println("查询结果为:"+dh);
i=2;
}
}
if(i!=2)System.out.println("查无此人");
xuanZe();
br.close();
}
public void tuiChu() {
System.out.println("谢谢使用! \n");
System.gc();// 系统回收资源
Runtime.getRuntime().runFinalization();// 没起作用;
}
public void finalize() {// 并未被调用
System.out.println("已回收资源,成功退出系统");
}
class ZiLiao {
private String name;
private String phoneNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String toString() {
String s = "姓名:" + name +"电话:" +phoneNumber;
return s;
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Main pb = new Main();
}
}
主要用到了io和集合;
运行效果
之前保存的信息,并没有丢失。