入门小白一枚,看了很多相关的内容才写出了这个系统,但还有一点的缺陷。
开始的解题思路是:直接通过IO流直接对文本进行操作,但是增删改的实现实在是太麻烦了,于是就推翻重来用了数组的读出写入的方式,进行相应的增删改操作再把数组的内容写入文本的方式。
最后还存在一点的问题是:对文本内删除内容,会存在一行的空白,在读取内容的时候依旧存在这个问题,如果有更好的建议可以再评论区或是私信里提出,完善好这个操作。
package day2;
import java.io.*;
import java.util.*;
public class ChengXu {
String path = "新建.txt";
public static void main(String[] args) {
ChengXu chengXu = new ChengXu();
// chengXu.test4("zhou");
// chengXu.test3();
chengXu.test();
}
// 显示
public void test() {
char c;
boolean quit = true;
while (quit) {
System.out.println("--------随机点名系统--------");
System.out.println("1.随机选人 2.添加学生 3.删除学生 4.查看信息 5.退出系统");
c = test6();
switch (c) {
case '1':
// 随机选人
test5();
break;
case '2':
// 添加学生
test3();
break;
case '3':
// 删除学生
System.out.println("请输入要删除的对象名字");
Scanner scanner = new Scanner(System.in);
test4(String.valueOf(scanner.next()));
break;
case '4':
// 查看信息
test2();
break;
case '5':
// 退出系统
char isExit = test7();
if (isExit == 'Y') {
quit = false;
}
break;
}
}
}
// 创建对象
public List test1() {
BufferedReader reader = null;
String lineTxt = null;
List list1 = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
list1 = new ArrayList<>();
int count;
// 逐行读取
while ((lineTxt = reader.readLine()) != null) {
list1.add(lineTxt);
}
reader.close();
} catch (Exception e) {
System.out.println(e);
}
return list1;
}
// 获取String数组
public void test2() {
for (String i : test1()) {
System.out.println(i);
}
}
// 添加
public void test3() {
List list = test1();
BufferedWriter writer = null;
Scanner sc = null;
try {
writer = new BufferedWriter(new FileWriter(path, true));
sc = new Scanner(System.in);
System.out.println("输入字符串:");
System.out.println("--姓名---年龄--学号--性别--专业--");
//next():只读取输入直到空格。
String str1 = sc.next();
var i2 = Integer.parseInt(sc.next());
var i3 = Integer.parseInt(sc.next());
String str4 = sc.next();
String str5 = sc.next();
//关闭
var input = new Information(str1, i2, i3, str4, str5);
writer.write(String.valueOf(input.toString())); // 逐行输入
writer.newLine(); // 换行
writer.flush(); // 刷新
} catch (IOException e) {
e.printStackTrace();
}
}
// 删除
public void test4(String name) {
if (name != null) {
BufferedWriter bufferedWriter = null;
List list = test1();
try {
bufferedWriter = new BufferedWriter(new FileWriter(path));
for (int i = 0; i < list.size(); i++) {
boolean b = list.get(i).contains(name);
if (b) {
Collections.replaceAll(list, list.get(i), "");
System.out.println("成功");
}
}
for (int i = list.size() - 1; i >= 0; i--) {
String delete = list.get(i);
if (delete.equals("")) {
break;
}
bufferedWriter.write(delete); //写入
bufferedWriter.newLine(); //换行
bufferedWriter.flush(); //刷新
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 随机抽取
public void test5(){
List strings = test1();
int index = new Random().nextInt(strings.size());
String name = strings.get(index);
System.out.println("被点到的人为:" + name);
}
// 检测输入内容是否符合
public static char test6() {
char c;
System.out.println("请输入操作: ");
Scanner scanner = new Scanner(System.in);
for (; ; ) {
String str = scanner.next();
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("6选择错误,请重新输入:");
} else break;
}
return c;
}
// 确认是否退出
public static char test7() {
Scanner scanner = null;
char c;
for (; ; ) {
System.out.println("确认是否退出 [Y]/[N]");
scanner = new Scanner(System.in);
String str = scanner.nextLine().toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("7选择错误,请重新输入:");
}
}
return c;
}
}
其中遇到了很多问题也都是从http://t.csdn.cn/cQx7G这篇文章上借鉴的,所以在内容上都会有相似的地方。



