我无法将内容打印到outfile中

时间:2015-01-22 14:48:58

标签: java printwriter

我似乎无法将要打印的内容打印到“transaction-list.txt”文件中。但它确实创建了文件,但在程序运行时它没有打印任何内容。它假设无缝地工作,只是不知道为什么 这是我的代码:

    final String acc_name= "Edward";
    final int acc_num=123456;
    final int acc_password=456789;
    boolean quit = false;
    int i;

    PrintWriter outFile = new PrintWriter("transaction-list.txt");
    outFile.println("HELLO!");

    //to output timestamp
    String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss").format(new Date());
    //System.out.print(timestamp);

    //ask user to key in the account number and password and stores it into acc_num and acc_password variables
    String stringAcc_num = JOptionPane.showInputDialog("Please enter your account number: ");
    int Acc_num = Integer.parseInt(stringAcc_num);

    String stringAcc_password = JOptionPane.showInputDialog("Please enter your account password: ");
    int Acc_password = Integer.parseInt(stringAcc_password);


    while (Acc_num != acc_num || Acc_password != acc_password){


        for (i = 1; i <= 2; i++) {

            String error = "YOUR ACCOUNT NAME AND YOUR ACCOUNT PASSWORD IS NOT A MATCH!!";
            JOptionPane.showMessageDialog(null, error, "ALERT!", JOptionPane.ERROR_MESSAGE);

            stringAcc_num = JOptionPane.showInputDialog("Please enter your account number: ");
            Acc_num = Integer.parseInt(stringAcc_num);

            stringAcc_password = JOptionPane.showInputDialog("Please enter your account password: ");
            Acc_password = Integer.parseInt(stringAcc_password);

            if (Acc_num == acc_num && Acc_password == acc_password)
            break;

        }//end for


        if (i > 2){
            JOptionPane.showMessageDialog(null, "NO MORE TIRES!", "ALERT!", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }//end if


     }//end while



    if (Acc_num == acc_num && Acc_password == acc_password){

        //to read the data from port-account.txt file
        Scanner readFile = new Scanner (new FileReader("port-account.txt"));

        //to create a txt file name "transaction-list.txt" 
        //PrintWriter outFile = new PrintWriter("transaction-list.txt");

        int current_balance = readFile.nextInt();

        do{

            //to pop out a message box
            String stringOption = "1. Transfer an account" + "\n2. List recent transactions" + "\n3. Display account details and current balance" + "\n4. Quit" + "\nPlease enter a number base on the following options above.";

            //to convert String into Int and stores it into "option"
            int option = Integer.parseInt(JOptionPane.showInputDialog(null, stringOption, "Menu options", JOptionPane.INFORMATION_MESSAGE));


            switch(option){ 
            case 1: 
                String Case1 = JOptionPane.showInputDialog("Please enter an amount to transfer: ");
                int amount_transfered = Integer.parseInt(Case1);

                int newCurrent_balance = current_balance - amount_transfered; //data from port-account.txt - user input amount

                JOptionPane.showMessageDialog(null, timestamp + "\nAmount transfered: $"+amount_transfered + "\nCurrent Balance: $"+newCurrent_balance, "Amount transfer complete!", JOptionPane.INFORMATION_MESSAGE);

                //print it to transaction-list.txt file
                outFile.println("Current Balance: $" + newCurrent_balance);
            break; 

            case 2: 
                System.out.print("testing123! testing123!");
            break; 

            case 3: 
                JOptionPane.showMessageDialog(null, "\nAccount Name: "+acc_name + "\nAccount Number: "+acc_num + "\nCurrent Balance: $"+current_balance, "Account Details", JOptionPane.INFORMATION_MESSAGE);
            break; 

            case 4: 
                System.exit(0);
            break; 


            default:
                JOptionPane.showMessageDialog(null, "The number you have input is invalid. Please try again.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
            }//end switch 

        }/*end do*/ while (!quit); //repeat do loop while "!quit"(not quit).

        outFile.close();
        readFile.close();

    }//end if

}
}

1 个答案:

答案 0 :(得分:1)

flush()上致电outFile

虽然Writer的javadoc告诉我们close()首先调用flush()BufferedWriter#close()中的代码(Writer的基础PrintWriter })实际上并不刷新缓冲区(而不是BufferedWriter#flush())。

所以从本质上讲,你只写一个缓冲区,它不一定会调用写入实际的OS输出流,而不会在写入器关闭时刷新。

摆脱System.exit()电话。

您还应该注意,如果您通过某个交换机分支上的System.exit()电话(通常是不好的做法)退出,则不会调用close()

<强> NB

如果您首先创建Minimal, Complete and Verifiable Example(这实际上是您的文件编写代码的单元测试),您可以轻松地自己解决这个问题。您也不会因为长时间的代码摘录而被投票,这与您遇到的问题基本无关。