【问题标题】:why doesnt my printWriter work ?为什么我的 printWriter 不起作用?
【发布时间】:2015-09-25 06:25:43
【问题描述】:

我不确定为什么这不起作用,我尽我所能。我觉得我非常接近做对了,但只是不能把最后几件事放在一起。我也意识到这不是很好的编码,但我是这方面的初学者,如果我能得到任何帮助,我将不胜感激。

        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();
            }





        }

【问题讨论】:

  • 是时候进行一些调试了,因为您必须先隔离错误,然后才能修复它。
  • 这是最小的?
  • 您是否将此ActionListener 添加到PrintTicket 组件中?也许source == PrintTicket 永远不是true
  • "this is the minimal" -- 完全不可运行且不可测试。请阅读或重新阅读第一条评论的链接。此外,"thats is the problem there is not errors when its running it simply doesnt do anything i want it to print the receipt into a new txt file but when the button is pressed it doesnt do anything, i only put the println part in there to make sure that the If statement was working" -- 调试不仅仅是修复编译错误。这意味着使用调试器并在运行时跟踪代码以查看逻辑错误在哪里。

标签: java swing jcombobox printwriter


【解决方案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?

【讨论】:

  • 我会说PrintStream(也许包装BufferedOutputStream)而不是BufferedWriter,因为这会让OP保留println语句,并且将具有相同的用例System.out(并且仍然在发生异常时抛出异常)
猜你喜欢
  • 2019-04-11
  • 2018-03-05
  • 2013-04-19
  • 2015-10-17
  • 2016-02-20
  • 2023-04-03
  • 2012-01-07
  • 2012-06-11
  • 2015-08-26
相关资源
最近更新 更多