weixin_55046612 2021-10-06 00:19 采纳率: 100%
浏览 49
已结题

文件写入出现问题了,能帮我看看改一下吗

在TeacherInformation.java这里的学生添加无法写入 学生.txt

package dzy;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class TeacherInformation {
    
    private String name;
    private String age;
    private String account;
    private String password;
    ArrayList<Student> array = new ArrayList<>();
    static Scanner scan=new Scanner(System.in);
    
    @Override
    public String toString() {
        return "TeacherInformation [name=" + name + ", age=" + age + ", account=" + account + ", password=" + password
                + "]";
    }
    
    
    
    public TeacherInformation() {
        super();
    }


    public TeacherInformation(String name, String age, String account, String password) {
        super();
        this.name = name;
        this.age = age;
        this.account = account;
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    void AdmLogin() {
        
        BufferedReader br = null;
        try {
            String str;
            br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
            while((str=br.readLine())!=null) {
                
                byte[] bytes = str.getBytes();
                str = new String(bytes,"UTF-8");
                String num = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String name = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String sid = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String dorm = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String classes = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String score = str.substring(0,str.indexOf(","));
                str = str.substring(str.indexOf(",")+1);
                String password = str;
                array.add(new Student(num,name,sid,dorm,classes,score,password));
        }
        }catch(Exception e1 ) {
            System.out.println("读取失败");
            e1.printStackTrace();
        }finally {
            if(br !=null) {
                try {
                    br.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
        
            
            while(true) {
        System.out.println("**************教师管理系统****************");
        System.out.println("\t 0:退出系统并保存\t");
        System.out.println("\t 1:查看个人信息\t");
        System.out.println("\t 2:修改密码\t");
        System.out.println("\t 3:添加学生信息\t");
        System.out.println("\t 4:查询某个学生的成绩.");
        System.out.println("\t 5.查询某个学生的信息");
        System.out.println("\t 6.查看所有学生的信息");
        System.out.println("\t 7:修改学生的成绩.");
        System.out.println("*****************************************");
        System.out.println("请输入0~7");
        String line=scan.nextLine();
        
        switch(line){
          case"1":
              System.out.println("用户名:"+name+"密码:"+password+"年龄:"+age+"账号:"+account);
              break;
          case"2":
              System.out.println("输入你的新密码:");
              String str = scan.nextLine();
              password = str;
              System.out.println("修改成功,新密码为:"+password);
              break;
          case"3":
                  addStudent();
                break;
          case"4":
              System.out.println("输入要查询学生的学号");
              String tsid = scan.nextLine();
              int i;
              for( i = 0;i<array.size();i++) {
                  if(tsid.equals(array.get(i).getSid())) {
                     break; 
                  }
              }
              if(i>=array.size()) {
                  System.out.println("学生不存在");
              }else {
                  System.out.println("该学生的成绩"+array.get(i).getScore());
              }
              break;
          case"5":
              System.out.println("输入查询学生的学号:");
              String tsid1 = scan.nextLine();
              int i1;
              for( i1 = 0;i1<array.size();i1++) {
                  if(tsid1.equals(array.get(i1).getSid())) {
                     break; 
                  }
              }
              if(i1>=array.size()) {
                  System.out.println("学生不存在");
              }else {
                  System.out.println("该学生的成绩:"+array.get(i1).toString());
              }
              break;
          case"6":
             
              for(int j = 0;j<array.size();j++) {
                  System.out.println("学生:"+array.get(j).getName()+"成绩:"+array.get(j).getScore());
              }
              break;
          case"7":
                  updateStudent();
                  break;
              
          case"0":
              
              try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            
              BufferedWriter wr = null;
                try {
                    String strs;
                    br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
                    while((str=br.readLine())!=null) {
                        System.out.println(str);
                        System.out.println(str.length());
                        byte[] bytes = str.getBytes();
                        str = new String(bytes,"UTF-8");
                        
                        String num = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String name = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String sid = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String dorm = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String classes = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String score = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String password = str;
                        array.add(new Student(num,name,sid,dorm,classes,score,password));
                }
                
                }catch(Exception e1 ) {
                    System.out.println("读取失败");
                    e1.printStackTrace();
                }finally {
                    if(br !=null) {
                        try {
                            br.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                }
              
              
                System.out.println("谢谢使用");
        
                System.exit(0);
                
                
          default:
                System.out.println("输入有误,请重新输入!");
            }
        
        }
    }
    
    
     void addStudent() {
        System.out.println("输入用户名:");
        String name =scan.nextLine();
        for(int i = 0 ;i<array.size();i++) {
            if(name.equals(array.get(i).getName())) {
                System.out.println("用户名已存在!");
                return;
            }
        }
        System.out.println("输入序号:");
        String num = scan.nextLine();
        System.out.println("输入学号:");
        String sid = scan.nextLine();
        System.out.println("输入宿舍:");
        String dorm = scan.nextLine();
        System.out.println("输入班级:");
        String classes = scan.nextLine();
        System.out.println("输入成绩:");
        String score = scan.nextLine();
        System.out.println("输入密码:");
        String password = scan.nextLine();
        
        array.add(new Student(num,name,sid,dorm,classes,score,password));
        System.out.println("添加成功!");
        
    }
    

    //修改学生信息
    public  void  updateStudent(){
        

        Scanner sc=new Scanner(System.in);
        if(array.size()==0){
            System.out.println("无信息,请先添加信息再查询!");
            return;
        
        }
                System.out.println("请输入你要修改的学生学号:");
                String sid=sc.nextLine();
                System.out.println("输入序号:");
                String num = scan.nextLine();
                System.out.println("输入姓名:");
                String name = scan.nextLine();
                System.out.println("输入宿舍:");
                String dorm = scan.nextLine();
                System.out.println("输入班级:");
                String classes = scan.nextLine();
                System.out.println("输入成绩:");
                String score = scan.nextLine();
                System.out.println("输入密码:");
                String password = scan.nextLine();
                
                Student s=new Student();
                s.setNum(num);
                s.setName(name);
                s.setSid(sid);
                s.setDorm(dorm);
                s.setCclass(classes);
                s.setScore(score);
                s.setPassword(password);
                
                for(int i=0;i<array.size();i++) {
                    Student student=array.get(i);
                    if(student.getSid().equals(sid)) {
                        array.set(i, s);
                        System.out.println("修改学生信息成功!");
                        return;
                    }
            }
                
                    System.out.println("输入学生信息有误,请重新输入!");
        
            
    }
}

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-10-06 00:43
    关注

    无法写入是有异常,还是写入没效果。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月6日

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频