如何将用户输入存储到文本文件中并显示所有数据?

时间:2016-12-01 18:29:50

标签: java

我们已设法获取代码以显示第一个员工的详细信息,但是,已显示其他2个员工详细信息。我不知道如何追加它们。我不确定printWriter是否是正确的代码,而不是那么,什么是最好的?

代码如下:)

public static void main(String[] args) throws IOException{
    Scanner scan = new Scanner(System.in);  
    File employeeDetails = new File("Employees.txt");
    PrintWriter pw = new PrintWriter(new FileWriter(employeeDetails, true));

    for(int i=0; i<3; i++){
        FileWriter fw = new FileWriter("Employees.txt", true);
        try{
            boolean repeat = false;

            System.out.println("Enter name: ");
            String name = scan.next();
            pw.println("name: " + name);

            System.out.println("Enter job title: ");
            String jobTitle = scan.next();
            pw.println("Job title: " + jobTitle);

            do{
                try{
                    System.out.println("Enter age: ");
                    int age = scan.nextInt();
                    pw.println("Age: " + age);
                    repeat = true;
                }
                catch(InputMismatchException ex){
                    System.err.println("Invalid age please enter a whole number."); 
                    scan.next();
                    continue;
                }
            }while(repeat==false);
            do{
                try{
                    System.out.println("Enter salary per year: ");
                    double salary = scan.nextDouble();
                    pw.println("Salary: " + salary);
                    repeat = false;
                }
                catch(InputMismatchException ex){
                    System.err.println("Invalid salary please enter a decimal."); 
                    scan.next();
                    continue;   
                } 
                catch(MissingFormatArgumentException ex){
                    System.err.println("Invalid salary please enter a decimal.");
                    scan.next();
                    continue;
                }
            }while(repeat);
        }finally{
            pw.close();
            fw.close();
        }
    }
    scan.close();
}

}

3 个答案:

答案 0 :(得分:0)

所以你的问题是每次进入for循环你都会创建一个新文件(删除另一个)

for(int i=0; i<3; i++){
        FileWriter fw = new FileWriter("Employees.txt", true);

把它放在外面

答案 1 :(得分:0)

您不需要fw

for(int i=0; i<3; i++){
    FileWriter fw = new FileWriter("Employees.txt", true);// remove this line

...

并删除此行:

fw.close();

否则你会得到NullPointerException

答案 2 :(得分:0)

你的问题是你继续关闭pw。在for循环后移动pw的关闭。我修改了您的代码,以下工作:

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);
    File employeeDetails = new File("Employees.txt");
    PrintWriter pw = new PrintWriter(new FileWriter(employeeDetails, true));

    for (int i = 0; i < 3; i++) {
        //FileWriter fw = new FileWriter("Employees.txt", true);
        try {
            boolean repeat = false;

            System.out.println("Enter name: ");
            String name = scan.next();
            pw.println("name: " + name);

            System.out.println("Enter job title: ");
            String jobTitle = scan.next();
            pw.println("Job title: " + jobTitle);

            do {
                try {
                    System.out.println("Enter age: ");
                    int age = scan.nextInt();
                    pw.println("Age: " + age);
                    repeat = true;
                } catch (InputMismatchException ex) {
                    System.err.println("Invalid age please enter a whole number.");
                    scan.next();
                    continue;
                }
            } while (repeat == false);
            do {
                try {
                    System.out.println("Enter salary per year: ");
                    double salary = scan.nextDouble();
                    pw.println("Salary: " + salary);
                    repeat = false;
                } catch (InputMismatchException ex) {
                    System.err.println("Invalid salary please enter a decimal.");
                    scan.next();
                    continue;
                } catch (MissingFormatArgumentException ex) {
                    System.err.println("Invalid salary please enter a decimal.");
                    scan.next();
                    continue;
                }
            } while (repeat);
        } finally {
            //pw.close();
            //fw.close();
        }
    }
    pw.close();
    scan.close();
}