为什么我的printWriter不起作用?

时间:2015-07-07 16:16:40

标签: java swing jcombobox printwriter

我不知道为什么这不起作用,我已经尽力了。我觉得我非常接近于正确但却无法将最后几件事情放在一起。我也意识到这是一个编码不太好但我是初学者,并希望得到任何帮助。

        public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        Object flight = AirlinesBox.getSelectedItem();
        Object DepartureLocation = LocationsBox.getSelectedItem();
        Object Destination = DestinationBox.getSelectedItem();
        Object ReturnOrSingle = TypeOfFlightBox.getSelectedItem();
        Object FlightDate = DateBox.getSelectedItem();
        Object FlightTime = TimeBox.getSelectedItem();
        Object FlightSeat = classOfSeatBox.getSelectedItem();
        Object FlightBag = FlightBagBox.getSelectedItem();
        Object FlightMeal = FlightMealBox.getSelectedItem();



        if(source == PrintTicket)
        {

            try {
                PrintWriter write = new PrintWriter("Receipt.txt");
                write.println("_______________________________________");
                write.println("|                                      |");
                write.println("|   --------------------------------   |");
                write.println("|   |  This is your Flight Receipt |   |");
                write.println("|   |      Please keep it safe     |   |");
                write.println("|   --------------------------------   |");
                write.println("|                                      |");
                write.println("|   --------------------------------   |");
                write.println("|   |                              |   |");
                write.println("     Airline= " + flight);
                write.println("     Departure Location= " + DepartureLocation);
                write.println("     Destination= " + Destination);
                write.println("     Flight Type= " + ReturnOrSingle);
                write.println("     Flight Day= " + FlightDate);
                write.println("     Flight Time= " + FlightTime);
                write.println("     Flight Seat Type= " + FlightSeat);
                write.println("     Extra Bags= " + FlightBag);
                write.println("     Flight Meal= " + FlightMeal);
                write.println("|   |                              |   |");
                write.println("|   --------------------------------   |");
                write.println("----------------------------------------");

                write.close();



                System.out.println(AirlinesBox.getSelectedItem());
                System.out.println(LocationsBox.getSelectedItem());
                System.out.println(DestinationBox.getSelectedItem());
                System.out.println(TypeOfFlightBox.getSelectedItem());
                System.out.println(DateBox.getSelectedItem());
                System.out.println(TimeBox.getSelectedItem());
                System.out.println(classOfSeatBox.getSelectedItem());
                System.out.println(FlightBagBox.getSelectedItem());
                System.out.println(FlightMealBox.getSelectedItem());

                }

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





        }

1 个答案:

答案 0 :(得分:1)

PrintWriter有一个讨厌隐藏错误的习惯。我建议交换到另一个读/写类。有关BufferedWriter的信息,请查看以下内容。或者,如果您遇到PrintWriter,请查看如何检查错误。

http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

请注意,您必须使用BufferedReader添加换行符,因为它没有println方法。有关差异的更多细节,请查看此帖。

Difference between java.io.PrintWriter and java.io.BufferedWriter?