如何在Java中将多行打印到.txt文件(外部)?

时间:2016-11-27 11:39:23

标签: java

我对Java编程比较陌生,所以这可能是一个菜鸟问题,但我已经为我的课程开发了一个电影票预订程序,其中一个要求是允许用户打印出来购买收据。我已成功将输出打印到.txt文件,但问题是所有信息都打印在一行中,但我希望它们以不同的行打印。有人可以帮我解决这个问题吗?

 private void printReceiptActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {
        // TODO add your handling code here:
        showMessageDialog(this, "Your receipt is printing!");

        //print receipt to a text file called Receipt
        FileWriter fw = new FileWriter("Receipt.txt");
        PrintWriter pw = new PrintWriter(fw);

        //calculation of prices
        float adultPrice = (float) (countA * 9.00);
        float studentPrice = (float) (countSt * 8.00);
        float seniorPrice = (float) (countSe * 7.00);
        float childPrice = (float) (countC * 5.00);

        //total price for tickets
        float totalPrice = adultPrice + studentPrice + seniorPrice + childPrice;

        try {
            String queryString = "SELECT title, movietheatre FROM movies WHERE movieid = " + movieID;
            ResultSet results = myStatement.executeQuery(queryString);

            NumberFormat formatter = NumberFormat.getCurrencyInstance();

            StringBuilder strBuilder = new StringBuilder();
            while (results.next()) {
                strBuilder.append("\t Cinema Ticket Receipt");
                strBuilder.append("\n");
                strBuilder.append("Movie Title : ").append(results.getString(1));
                strBuilder.append("\n");
                strBuilder.append("Time : ").append(time);
                strBuilder.append("\n");
                strBuilder.append("Date : ").append(date);
                strBuilder.append("\n");
                strBuilder.append("Movie Hall : ").append(results.getString(2));
                strBuilder.append("\n");
                strBuilder.append("\n");
                strBuilder.append("Adult ticket price : ").append(formatter.format(adultPrice));
                strBuilder.append("\n");
                strBuilder.append("Student ticket price : ").append(formatter.format(studentPrice));
                strBuilder.append("\n");
                strBuilder.append("Seniors ticket price : ").append(formatter.format(seniorPrice));
                strBuilder.append("\n");
                strBuilder.append("Children ticket price : ").append(formatter.format(childPrice));
                strBuilder.append("\n");
                strBuilder.append("\n");
                strBuilder.append("Total price : £").append(formatter.format(totalPrice));
                strBuilder.append("\n");
            }                             
            results.close();
            //String formatStr = strBuilder.toString();
            //out.write(String.format(formatStr));


            pw.print(strBuilder.toString());
            pw.close();
        } catch (SQLException sqle) {
            out.println(sqle);
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        Logger.getLogger(Receipt.class.getName()).log(Level.SEVERE, null, ex);
    }
    showMessageDialog(this, "Receipt printing complete.");
} 

0 个答案:

没有答案