PrintWriter not writing to file (Java)

前端 未结 2 628
失恋的感觉
失恋的感觉 2021-01-23 10:51

I am writing a kind of a cash machine program that will output the data into a file (Yes, I know its not in English, but that\'s not the point) and I am experiencing an error.

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 11:01

    public static void studentAdd() throws IOException ,ClassNotFoundException{
    
        DataOutputStream output = new DataOutputStream(new FileOutputStream("Student.txt"));
        PrintWriter pr = new PrintWriter("Student.txt");
        Student7 student[] = new Student7[total];
        int STOP = 999;
        int stdID;
        int age;
        String address;
        String gender;
        String name;
    
        Scanner input = new Scanner(System.in);
        System.out.println("Please type  student ID or 999 to quit");
        stdID = input.nextInt(); 
    
        try {
            while(stdID != STOP) {  
                input.nextLine();
                output.writeInt(stdID);
                pr.print(stdID + "#");
    
                System.out.println("Please type student name");
                name = input.nextLine();
                output.writeUTF(name);
                pr.print(name + "#");
    
                System.out.println("Please type student age");
                age = input.nextInt();
                output.writeInt(age);
                input.nextLine();
                pr.print(age + "#");
    
                System.out.println("Please type student gender");
                gender = input.nextLine();
                output.writeUTF(gender);
                pr.print(gender + "#");                     
    
                System.out.println("Please type student address");
                address = input.nextLine();
                output.writeUTF(address);
                pr.print(address + "#");
                pr.println();
    
                System.out.println("Please type  student ID or 999 to quit");
                stdID = input.nextInt();
                }
            ;   pr.close();
                output.close();
    
        } catch (IOException e) { 
            System.err.println("Error is" + e.getMessage());
        } catch(InputMismatchException e) {
            System.err.println("Invalid Data Entry");
        }finally {
            studentMenu();
        }
    
    } //end of stuadd
    

提交回复
热议问题