使用IO流实现电话本管理系统
案例要求(IO流)
将电话本信息,写入文件,并能实现增删改查功能。
示例代码
package com.ambow.day0509;
import java.io.*;
import java.util.Scanner;
public class PhoneSys {
static Scanner scanner = new Scanner(System.in);
static File phones = new File("D:" + File.separator + "phones.txt");
//主方法
public static void main(String[] args){
//文件不存在,则创建
if (!phones.exists()){
try {
phones.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
while(true) {
System.out.println("*******电话本管理系统******");
System.out.println("*******1.添加联系人******");
System.out.println("*******2.修改联系人******");
System.out.println("*******3.删除联系人******");
System.out.println("*******4.查询联系人******");
System.out.println("*******0.退出通讯录******");
System.out.println("请选择:");
int choose = scanner.nextInt();
switch (choose) {
case 1:
addContacts();//添加联系人
break;
case 2:
updateContacts();//修改联系人
break;
case 3:
deleteContacts();//删除联系人
break;
case 4:
queryContacts();//查询联系人
break;
case 0:
System.out.println("退出系统");
System.exit(0);
break;
default:
System.out.println("您的选择有误!");
break;
}
}
}
//查询所有联系人
private static void queryContacts() {
System.out.println(readFile());
}
//删除联系人
private static void deleteContacts() {
System.out.println("请输入要删除的联系人姓名:");
String name = scanner.next();
//获取所有联系人信息
String allContacts = readFile();
int nameIndex = allContacts.indexOf(name);
if (nameIndex == -1){
System.out.println("查无此人");
return;
}
int endIndex = allContacts.indexOf("\n",nameIndex);
String info = allContacts.substring(nameIndex,endIndex + 1);
allContacts = allContacts.replaceAll(info,"");
updateFile(allContacts);
System.out.println("删除成功!");
}
//大家自己实现
private static void updateContacts() {
}
//添加联系人
private static void addContacts() {
StringBuilder info = new StringBuilder();
System.out.println("姓名:");
String name = scanner.next();
System.out.println("性别:");
String sex = scanner.next();
System.out.println("手机号码:");
String phone = scanner.next();
info.append(name).append(";").append(sex).append(";").append(phone);
addToFile(info.toString());
}
//读文件
public static String readFile(){
StringBuilder stringBuilder = new StringBuilder();
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(phones);
br = new BufferedReader(fr);
String s = null;
while ((s = br.readLine()) != null){
//特别注意:只加\n不能实现换行,要换行,需要加\r\n,表示回车换行。
stringBuilder.append(s).append("\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
//添加联系人到文件
public static void addToFile(String info){
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(phones,true);
bw = new BufferedWriter(fw);
bw.write(info);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//更新文件
public static void updateFile(String all){
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(phones);
bw = new BufferedWriter(fw);
bw.write(all);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:以上修改联系人功能未实现,可参考删除联系人自行实现。