全程无报错 每步都有注解
因为防止 main主代码 太长 将很多方法都封装了
编程小白 有不足的地方希望能指正出来吧
我觉得自己比较好的地方是 将页面方法返回值 设置为集合 这样打印页面的时候 能返回路径名
后面很多需要路径名的地方,直接将打印页面的方法带进去就好了
使用的时候记得上下翻一翻 点个赞再走吧
package StudentNameTest;
import java.io.*;
import java.util.*;
/**
* @ClassName StudentNameTest.subject
* @Description TODO
* @Author CC
* @DATE 2022/5/17 17:01
* @Version 1.0
*/
public class subject {
public static void main(String[] args) throws Exception {
subject subject = new subject();//创建对象
subject.method(); //调用方法
}
//输入调用方法
private void method() {
boolean loopFlag =true; //循环条件 便于终止
char menu;//判断输入字符
while (loopFlag){
mainMenu(); //主界面打印
//输入数字
menu = getOutChar();
switch (menu){
case '1':
//随机选人
getStudent();
System.out.println();
break;
case '2':
//添加学生
System.out.println("请输入要添加的学生姓名");
addStudent();
System.out.println();
break;
case '3':
//删除学生
System.out.println("请输入要删除的学生姓名");
deleteStudent();
System.out.println();
break;
case '4':
//系统退出
System.out.println();
System.out.print("确认是否退出(Y/N): ");
char isExit = getOut();
if (isExit == 'Y'||isExit == 'y'){
loopFlag = false;
}
break;
case '5':
//查看所有学生名单
System.out.println();
System.out.println("系统所有学生如下: ");
getAllStudent();
break;
default:
System.out.println("请重新输入(1-4)的数字");
}
}
}
//主界面
//返回一个路径
private String mainMenu(){
System.out.println("-----------------欢迎进入随机选人系统-------------------");
System.out.println("1-随机选人");
System.out.println("2-添加学生");
System.out.println("3-删除学生");
System.out.println("4-退出");
System.out.println("5-查看所有学生");
System.out.print("文件路径名:");
String FileAccount = "D:\\Desktop\\StudentName.txt";
System.out.println(FileAccount);
return FileAccount;
}
//随机抽人
private void getStudent(){
ArrayList<String> array = getSetMethod();
Set<String> set = new HashSet<>(array);
array = new ArrayList<>(set); //除去集合中重复的元素
//防止选到换行的空的
while (true){
//获取一个随机数
Random r = new Random();
int index = r.nextInt(array.size());
String name = array.get(index); //抽到的名字
if (name.length() != 0) {
System.out.println("幸运的同学是:" + name);
break;
}
}
}
//删除学生
//打算全部 取到集合中 删除 再重写入
private void deleteStudent(){
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(mainMenu())); //缓冲读取字符流
bufferedWriter = new BufferedWriter(new FileWriter(mainMenu())); //缓冲写入字符流
ArrayList<String> arrayList = new ArrayList<>(); //创建一个集合
//写到集合中
String line;
while ((line = bufferedReader.readLine())!=null){
arrayList.add(line);
}
String next = new Scanner(System.in).next(); //键盘输入 要删除的姓名
boolean b = Collections.replaceAll(arrayList, next, "");//替换成空字符
if (b){
System.out.println("删除成功");
}else {
System.out.println("文件没有此学生");
}
//循环遍历集合写入
for(int i=arrayList.size()-1;i>=0;i--){
String delete = arrayList.get(i);
bufferedWriter.write(delete); //写入
bufferedWriter.newLine(); //换行
bufferedWriter.flush(); //刷新
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert bufferedWriter != null;
bufferedWriter.close(); //关闭
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//添加学生
private void addStudent() {
BufferedWriter bufferedWriter = null; //缓冲字符流
try {
bufferedWriter = new BufferedWriter(new FileWriter(mainMenu(),true));
String name = new Scanner(System.in).nextLine(); //键盘输入要添加的数据
bufferedWriter.newLine(); //空行
bufferedWriter.write(name); //写入
bufferedWriter.newLine();//空行
bufferedWriter.flush(); //刷新
System.out.println("添加学生成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert bufferedWriter != null;
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//键盘输入数字
private char getOutChar(){
char c ;
Scanner scanner = new Scanner(System.in);
while (true) {
String s = scanner.next();
c = s.charAt(0);
if (c == '1' || c == '2' || c == '3' || c == '4'||c == '5') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
//键盘输入字符
private char getOut() {
char c;
Scanner scanner = new Scanner(System.in);
while (true) {
String s = scanner.nextLine();
c = s.charAt(0); //String - char
if (c == 'Y' || c == 'N' || c == 'y' || c == 'n') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
//获取文件所有学生名单
private void getAllStudent(){
ArrayList<String> array = getSetMethod();
Set<String> set = new HashSet<>(array);
for (String s : set) {
System.out.print("[ ");
System.out.print(s);
System.out.println(" ]");
}
}
//将文件入存集合方法
private ArrayList<String> getSetMethod(){
BufferedReader br = null;
ArrayList<String> array = null;
try {
br = new BufferedReader(new FileReader(mainMenu()));//存储文件的路径
array = new ArrayList<>(); //创造集合 写入集合
String line;
while ((line = br.readLine())!=null){
array.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert br != null;
br.close(); //关闭
} catch (IOException e) {
e.printStackTrace();
}
}
return array;
}
}