java随机选人,实现对文本文件操作

理解

全程无报错 IDEA全绿通过 每步都有注解

因为防止 main主代码 太长 将很多方法都封装了

编程小白 有不足的地方希望能指正出来吧

好的处理点

我觉得自己比较好的地方是 将页面方法返回值 设置为集合 这样打印页面的时候 能返回路径名

后面很多需要路径名的地方,直接将打印页面的方法带进去就好了

如果要用的话 使用自己的TXT文件,在打印页面方法,那里添加自己的路径就好啦

能实现 随机抽人 添加姓名 修改姓名 删除姓名 比较简单但是基本功能都有

麻雀虽小五脏俱全就是这样

利用的是java的IO流

集合进行操作

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;
    }
}

报错内容

ErrorCode=0 
SQLState=01S00 
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

报错原因:

在使用mysql的jdbc驱动最新版(6.0+)时,遇到数据库和系统时区差异引起的问题。

解决方法0-使用6.0以下版本的jdbc:

降版本,并不推荐;

解决方法1-在jdbc url指定默认时区:

还有一种是在jdbc连接的url后面加上serverTimezone=UTC或GMT即可,如果指定使用gmt+8时区,需要写成GMT%2B8,否则可能报解析为空的错误。示例如下:

jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值