任务需求2:
电话本信息包括:姓名、性别、电话号码,将电话本信息写到文件中,并能够进行操作:
1) 将信息全部读到内存中,在内存中进行增删改操作,然后再重写文件;
2)电话本信息写到文件时,每个属性间添加符号,使用split分割
所需知识点:
1.运用File类进行文件和目录操作;
2.用字节流、字符流实现文件的读写;
代码如下:
package cn.demo.main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
JavaDemo();
}
private static void JavaDemo() throws IOException, FileNotFoundException {
File file = new File("D:\\homework\\phone.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
int newFlag = 0 ;
while(newFlag==0){
newFlag = MainDemo(file, bw, newFlag);
}
}
private static int MainDemo(File file, BufferedWriter bw, int newFlag)
throws IOException, FileNotFoundException {
Demo();
Scanner scr = new Scanner(System.in);
int number = scr.nextInt() ;
if(number==1){
Demo1(scr, bw);
}else if(number==2){
Demo2(file, scr);
}else if(number==3){
Demo3(file, scr);
}else if(number==4){
Demo4(file, scr);
}else{
newFlag = 1 ;
System.out.println("退出!");
}
return newFlag;
}
private static void Demo4(File file, Scanner scr)
throws FileNotFoundException, IOException {
System.out.println("--查询界面--");
System.out.println("全部信息如下:");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine() ;
StringBuffer sbr = new StringBuffer() ;
while(line!=null){
sbr.append(line);
line = br.readLine() ;
if(line!=null){
sbr.append("+");
}
}
String[] arr = sbr.toString().split("\\+");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
System.out.println("输入所要查询的名字:");
String namePeople = scr.next() ;
int flag = 0 ;
for(int i=0;i<arr.length;i++){
String[] arr1 = arr[i].split(",");
if(namePeople.equals(arr1[0])){
System.out.println("姓名:"+arr1[0]);
System.out.println("性别:"+arr1[1]);
System.out.println("电话:"+arr1[2]);
flag = 1 ;
}
}
if(flag==0){
System.out.println("没有此人信息!");
}
}
private static void Demo3(File file, Scanner scr)
throws FileNotFoundException, IOException {
System.out.println("--修改界面--");
System.out.println("输入需要修改的姓名:");
String name = scr.next();
System.out.println("输入需要修改的年龄:");
String age = scr.next() ;
System.out.println("输入需要修改的电话号码:");
String tele = scr.next() ;
String str = name + "," + age + "," + tele ;
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine() ;
StringBuffer sbr = new StringBuffer() ;
while(line!=null){
sbr.append(line);
line = br.readLine() ;
if(line!=null){
sbr.append("+");
}
}
int flag = 0 ;
String[] arr = sbr.toString().split("\\+");
File file1 = new File("D:\\homework\\phone.txt");
FileWriter fw1 = new FileWriter(file1);
BufferedWriter bw1 = new BufferedWriter(fw1);
for(int i=0;i<arr.length;i++){
if(arr[i].equals(str)){
System.out.println("请输入修改的信息:");
System.out.println("新姓名:");
String newName = scr.next();
System.out.println("新年龄:");
String newAge = scr.next() ;
System.out.println("新电话号码:");
String newTele = scr.next() ;
String newPeople = newName + "," + newAge + "," + newTele ;
bw1.write(newPeople);
bw1.newLine();
bw1.flush();
flag = 1;
}else{
bw1.write(arr[i]);
bw1.newLine();
bw1.flush();
}
}
if(flag==0){
System.out.println("没有此人信息!");
}
}
private static void Demo2(File file, Scanner scr)
throws FileNotFoundException, IOException {
System.out.println("--删除界面--");
System.out.println("输入删除的姓名:");
String name = scr.next();
System.out.println("输入删除的年龄:");
String age = scr.next() ;
System.out.println("输入删除的电话号码:");
String tele = scr.next() ;
String str = name + "," + age + "," + tele ;
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine() ;
StringBuffer sbr = new StringBuffer() ;
while(line!=null){
sbr.append(line);
line = br.readLine() ;
if(line!=null){
sbr.append("+");
}
}
int flag = 0 ;
String[] arr = sbr.toString().split("\\+");
File file1 = new File("D:\\homework\\phone.txt");
FileWriter fw1 = new FileWriter(file1);
BufferedWriter bw1 = new BufferedWriter(fw1);
for(int i=0;i<arr.length;i++){
if(!arr[i].equals(str)){
bw1.write(arr[i]);
bw1.newLine();
bw1.flush();
flag = 1 ;
}
}
if(flag==0){
System.out.println("没有此人信息!");
}
}
private static void Demo() {
System.out.println("电话本信息管理");
System.out.println("1.增加 , 2.删除 ,3.修改 ,4.查询 ,5.退出");
System.out.println("请输入您的选择:");
}
private static void Demo1(Scanner scr, BufferedWriter bw)
throws IOException {
System.out.println("--增加界面--");
System.out.println("输入添加的数量:");
int n = scr.nextInt() ;
for(int i=0;i<n;i++){
System.out.println("姓名:");
String name = scr.next();
System.out.println("年龄:");
String age = scr.next() ;
System.out.println("电话号码:");
String tele = scr.next() ;
String str = name + "," + age + "," + tele ;
bw.write(str);
bw.newLine();
bw.flush();
}
}
}