0

很抱歉下面的代码很长,当用户输入选项“5”时,我的代码能够正常执行和运行。我已经从用户输入中获取了所有值并且没有错误,但它没有写入我的文件。

我的文件目前有数据:

Anthony Ducan;anthony;a123;55 Peter Street;3321444;VISA;3213504011223
Barry Blake;barry;a999;456 George Street;23239876;VISA;435677779876
Claire Rerg;clare;c678;925 Edward Lane;67893344;MASTERCARD;223344556677

我正在尝试以与我的文本文件完全相同的方式存储。我的void writeLinesToFile()方法编码有问题还是void newCust()方法有问题?

仍然对我没有错误感到困惑,但它没有写入我的文件。

public MainPage1(){ //start of MainPage1()

        System.out.println("=====================================================");
        System.out.println("Kreg Hotel Booking System - Main Page");
        System.out.println("=====================================================");
        System.out.println("[1] General Information");
        System.out.println("[2] Make Booking");
        System.out.println("[3] Active Booking Summary");
        System.out.println("[4] Existing Customer");
        System.out.println("[5] New Customer");
        System.out.println("[6] Check Booking Status");
        System.out.println("[7] Promotions");
        System.out.println("[8] Exit\n");
        System.out.println("Note: You have to select option 2 & 3 before option 4 & 5\n");
        System.out.println("Please enter your selection: ");        

try{choice = input.nextInt();}//to try to see if user input integer

         catch (java.util.InputMismatchException e){//to catch if user didnt input integer  
            System.out.println("Invalid Input");
            return;
        }


        while(true){ //start of do-while loop

            if(choice==4||choice==5)
            {
                if(roomInfo.isEmpty()==true || addOns.isEmpty()== true){
                    System.out.println("Please enter option 2 or 3 before option 4 or 5");
                    choice=input.nextInt();
                }
                else if(choice == 4){existingCustomers();}
                else if(choice ==5){newCust();}
                else new MainPage1();               
            }

        switch(choice){

                case 1:break;
                case 2:break;
                case 3:break;
                case 4:break;
                case 5:newCust();
                        break;
                case 6:break;
                case 7:break;
                case 8:System.out.println("Thank you. See you again soon");//exit the menu
                        System.exit(0);break;
                default:System.out.println("Please enter number between 1 to 8");//prompts the user if they didnt enter between 1 to 8
                       choice = input.nextInt();
                       break;
            }

        }//while(choice!=1 || choice !=2 || choice!=3 ||choice!=4); // end of do-while loop



    }//end of MainPage1()


public static void main(String[] args){//start of main page


    new MainPage1();//to call the constructor


}//end of mainpage



public void writeLinesToFile(String filename,String[] linesToWrite,boolean appendToFile){

    PrintWriter pw = null;

    try {

      if (appendToFile) {

        //If the file already exists, start writing at the end of it.
        pw = new PrintWriter(new FileWriter(filename, true));

      }
      else {

        pw = new PrintWriter(new FileWriter(filename));
        //this is equal to:
        //pw = new PrintWriter(new FileWriter(filename, false));

      }

      for (int i = 0; i < linesToWrite.length; i++) {

        pw.println(linesToWrite[i]+";");

      }
      pw.flush();

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {

      //Close the PrintWriter
      if (pw != null)
        pw.close();

    }

  }

  public void newCust(){

    System.out.println("Welcome to Kreg Hotel Booking System");
    System.out.println("==============================================");
    System.out.println("Please enter your name: ");
    n = input.nextLine();
    n = input.nextLine();
    System.out.println("Please enter your login username: ");
    un = input.nextLine();
    System.out.println("Please enter your login password: ");
    pw = input.nextLine();
    System.out.println("Please enter your address: ");
    a = input.nextLine();
    System.out.println("Please enter your contact: ");
    con = input.nextLine();
    System.out.println("Please enter your credit card type: ");
    ct = input.nextLine();
    System.out.println("Please enter your credit card number: ");
    cn = input.nextLine();
    MainPage1 util = new MainPage1();
     util.writeLinesToFile("customerinfo.txt",new String[]{n,un,pw,a,con,ct,cn},true);
     new MainPage1();

  }
}//end of class
4

1 回答 1

0

查看FileBufferedWriterFileWriter文档。你应该能够用这两个类来完成这个。此代码应该可以帮助您入门。

String content = "Add this everytime";

File file =new File("example.txt");

//if file does not exists, then create it
if(!file.exists()){
   file.createNewFile();
}

//true = append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(content);
bufferWritter.close();

System.out.println("Done");
于 2012-07-30T20:11:02.117 回答