【问题标题】:I can't print things into outfile我无法将内容打印到 outfile
【发布时间】:2015-01-22 14:48:58
【问题描述】:

我似乎无法将要打印的内容打印到“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

}
}

【问题讨论】:

  • 这是一个相当长的代码。你到底是在哪里打印的?你在刷新输出流吗?
  • 我似乎无法将要打印的内容打印到“transaction-list.txt”文件中。而且我不知道为什么,它应该有效。
  • 代码中的哪一行应该打印到文件中?
  • 而不是 system.exit 在选项 4 中退出循环。我想这可以完成这项工作。
  • outFile.println("你好!");和 outFile.println("Current Balance: $" + newCurrent_balance).....它不会在 transaction-list.txt 文件中打印任何内容

标签: java printwriter


【解决方案1】:

在您的outFile 上致电flush()

虽然Writer 的javadoc 告诉我们close() 首先调用flush(),但BufferedWriter#close() 中的代码(PrintWriter 的底层Writer)实际上并没有刷新缓冲区(相反到BufferedWriter#flush())。

所以本质上你只写入一个缓冲区,这并不一定会调用写入实际的操作系统输出流,它不会在编写器关闭时被刷新。

摆脱System.exit()电话。

您还应该注意,如果您在其中一个 switch 分支上通过 System.exit() 调用(通常是一种不好的做法)退出,即使 close() 也不会被调用。

注意

如果您首先创建一个Minimal, Complete and Verifiable Example,您可以轻松解决这个问题(这实质上是您的文件编写代码的单元测试)。您也不会因为与您遇到的问题在很大程度上无关的长段代码而被否决。

【讨论】:

  • 对不起,我还是Java新手,我还没有了解你刚才提到的BufferedWriter。
  • 当快捷方式通过 System.exit() 退出时,根本不会调用 close() ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2013-11-06
相关资源
最近更新 更多