遁去一
2021-09-08 01:41
采纳率: 100%
浏览 46
已结题

为什么我的代码始终不能写入文件中?求大家看看有什么问题嘛?



```java
package testStudentForIO;

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

public class StudentDemo {
    public static void main(String[] args) throws Throwable {
        
        ArrayList<Student> array = new ArrayList<Student>();
        String fileName = "Student.txt";
        
        while(true){
            
            System.out.println("欢迎来到学生管理系统!");
            System.out.println("1、查看现有学生");
            System.out.println("2、添加学生信息");
            System.out.println("3、修改学生信息");
            System.out.println("4、删除学生信息");
            System.out.println("5、退出系统!");
            
            Scanner sc =new Scanner(System.in);
            System.out.println("请输入您要进行的操作");
            String number = sc.nextLine();
            
            switch(number){
            case "1":
                findStudent(fileName);
                break;
            case "2":
                addStudent(fileName);
                break;
            case "3":
                changeStudent(fileName);
                break;
            case "4":
                deleteStudent(fileName);
                break;
            case "5":
                System.out.println("感谢您的操作!再见!");
                System.exit(0);
                break;
            default:
                    System.out.println("对不起,您的输入有误,请重新输入:");
                break;
            }
            
            
            
        }
    }
    
    //从文件读取到集合
    public static void arrayToFile(String fileName,ArrayList<Student> array) throws IOException{
        
        //ArrayList<Student> array = new ArrayList<Student>();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        
        String line;
        while((line = br.readLine()) != null){
            String[] atr = new String[1024];
            atr = line.split(",");
            Student s = new Student();
            
            s.setId(atr[0]);
            s.setName(atr[1]);
            s.setAge(atr[2]);
            s.setAddress(atr[3]);
            
            array.add(s);
            
            
        }
        br.close();
        
    }
    
    //从集合写入到文件
    public static void fileToArray(String fileName, ArrayList<Student> array) throws IOException{
        
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
        
        for (int i = 0; i < array.size(); i++) {
            Student s = new Student();
            s = array.get(i);
            StringBuilder sb = new StringBuilder();
            sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
            
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
    
    //查看所有学生:
    public static void findStudent(String fileName) throws IOException{
        
        ArrayList<Student> array = new ArrayList<Student>();
        arrayToFile(fileName,array);
        
        if(array.size() == 0){
                System.out.println("对不起,当前系统无学生信息!");
                return;
            }else{
                System.out.println("学号,姓名,年龄,居住地");
                for (int j = 0; j < array.size(); j++) {
                    Student s = array.get(j);
                    System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
                    
                }
            }
    }
    
    //添加学生信息
    public static void addStudent(String fileName) throws IOException{
        
        ArrayList<Student> array = new ArrayList<Student>();
        arrayToFile(fileName,array);
        
        Scanner sc = new Scanner(System.in);
        
        String id;
        
        while(true){
            System.out.println("请输入学生学号:");
            id = sc.nextLine();
            
            boolean flag = false;
            for (int i = 0; i < array.size(); i++) {
                Student s = array.get(i);
                
                if(s.getId().equalsIgnoreCase(id)){
                    flag = true;
                    break;
                }
            }
            
            if(flag){
                System.out.println("对不起该学号已被占用,请重新输入!");
            }
            break;
        }
        System.out.println("请输入学生姓名:");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄:");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地:");
        String address = sc.nextLine();
        
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        
        array.add(s);
        
        arrayToFile(fileName,array);
        
        System.out.println("添加学生成功!");
        
        
        
        
    }
    
    //修改学生信息:
    public static void changeStudent(String fileName) throws IOException{
        

        ArrayList<Student> array = new ArrayList<Student>();
        arrayToFile(fileName,array);
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号:");
        String id = sc.nextLine();
        
        while(true){
            
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Student s = new Student();
                s = array.get(i);
                
                if(s.getId().equalsIgnoreCase(id)){
                    index = i;
                    break;
                }
                
            }
            
            if(index == -1){
                System.out.println("对不起没有找到该学号");
                return;
            }else{
                System.out.println("请输入学生姓名:");
                String name = sc.nextLine();
                System.out.println("请输入学生年龄:");
                String age = sc.nextLine();
                System.out.println("请输入学生居住地:");
                String address = sc.nextLine();
                
                Student s = new Student();
                s.setId(id);
                s.setName(name);
                s.setAge(age);
                s.setAddress(address);
                
                array.set(index, s);
                
                arrayToFile(fileName,array);
                
                System.out.println("添加学生成功!");
                
            }
            
        }
        
    }
    
    //删除学生信息
    public static void deleteStudent(String fileName) throws IOException{
        ArrayList<Student> array = new ArrayList<Student>();
        arrayToFile(fileName,array);
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号:");
        String id = sc.nextLine();
        
        while(true){
            
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Student s = new Student();
                s = array.get(i);
                
                if(s.getId().equalsIgnoreCase(id)){
                    index = i;
                    break;
                }
                
            }
            
            if(index == -1){
                System.out.println("对不起没有找到该学号");
                return;
            }else{
                
                
                array.remove(index);
                
                arrayToFile(fileName,array);
                
                System.out.println("添加学生成功!");
                
            }
            
        
    }
    
    
    
    }    
    
    
    
}


```

  • 好问题 提建议
  • 收藏

2条回答 默认 最新

  • 两个猕猴桃 2021-09-08 06:54
    已采纳

    166行
    arrayToFile改成fileToArray

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • cnkeysky 2021-09-08 06:13

    写入文件这个方法fileToArray,你没调用

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题