java while循环尝试catch,无法打印到新行

时间:2014-10-29 05:04:44

标签: java

在这里遇到问题,我需要这个循环来打印新的代码行到文件,但它的作用是打印1行然后在第二轮失败,

永远无法将其打印到另一行,下面是代码

public class study  {
    public static void main(String[] args) throws IOException{
         BufferedWriter post =  null;
         File file = new File("text.txt");

            if(!file.exists()){
                file.createNewFile();
            }
            boolean promptUser = true;
            FileWriter fileWriter = new FileWriter(file);
            post = new BufferedWriter(fileWriter);
            try {
                while(promptUser){
                    System.out.println("enter age ");   //get age   
                    Scanner getage = new Scanner(System.in);
                    int age= getage.nextInt();

                    if(age <20 || age>50){ //age range
                        System.out.println("age must be between 20 and 50");
                        System.exit(0);
                    }
                    System.out.println("enter name ");      //get name
                    Scanner getname = new Scanner(System.in);       
                    String name= getname.nextLine();

                    System.out.println("enter email "); //get email 
                    Scanner getarea = new Scanner(System.in);       
                    String email= getarea.nextLine();

                    post.write(age + "\t"); <===== fails here on second run
                    post.write(name + "\t");                    
                    post.write(email + "\t");
                    post.newLine();
                    post.close();

                    System.out.println("enter quit to quit or any key to continue");
                    Scanner options = new Scanner(System.in);   
                    String option = options.nextLine();     

                    if(option.equalsIgnoreCase("quit")){            
                            System.out.println("goodbye!");
                            System.exit(0);
                    } 
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

            post.write(age + "\t"); 
            post.newLine();
            post.write(name + "\t");                    
            post.newLine();
            post.write(email + "\t");
            post.newLine();

//删除post.close();从这里 现在它可以解决你的问题

答案 1 :(得分:0)

post.close();替换为post.flush();,您应该没问题。

输入退出条件时关闭流。

答案 2 :(得分:0)

FIXED IT GUYS

我需要将FileWriter行移出TRY

import java.io.*;
import java.util.*;

public class study {
    public static void main(String[] args) throws IOException {
        BufferedWriter post =  null;

        File file = new File("text.txt"); //create file

        if (!file.exists())
        {
              file.createNewFile();
        }

        boolean promptUser = true;
        FileWriter fileWriter = new FileWriter(file);

        try {
            while (promptUser) {
                post = new BufferedWriter(fileWriter);
                System.out.println("enter age "); // get age    
                Scanner getage = new Scanner(System.in);        
                int age = getage.nextInt();

                if (age < 20 || age > 50){ //age range
                    System.out.println("age must be between 20 and 50");
                    System.exit(0);
                }   

                System.out.println("enter name "); //get name
                Scanner getname = new Scanner(System.in);       
                String name= getname.nextLine();

                System.out.println("enter email "); // get email    
                Scanner getarea = new Scanner(System.in);       
                String email= getarea.nextLine();

                //send data to file
                post.write(age + ";");
                post.write(name + ";");                 
                post.write(email + ";");
                post.newLine();
                post.flush();

                System.out.println("enter quit to quit or any key to continue");
                Scanner options = new Scanner(System.in);   
                String option = options.nextLine();     

                if (option.equalsIgnoreCase("quit")) {
                    System.out.println("goodbye!");
                    post.close(); // close file upon quitting
                    System.exit(0);
                }

            }
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }
}