为什么我的 printWriter 不工作?
why doesnt my printWriter work ?
我不确定为什么这不起作用,我已经尽我所能。我觉得我非常接近做对了,但就是无法将最后几件事放在一起。我也意识到这是一个不太好的编码,但我是这方面的初学者,希望能得到任何帮助。
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();
}
}
PrintWriter 有向您隐藏错误的恶习。我建议换成另一个 read/write class。请查看以下内容以获取有关 BufferedWriter 的信息。或者,如果您受困于 PrintWriter,请查看如何检查错误。
http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
请注意,您必须使用 BufferedReader 添加换行符,因为它没有 println 方法。有关差异的更多详细信息,请查看此 post.
Difference between java.io.PrintWriter and java.io.BufferedWriter?
我不确定为什么这不起作用,我已经尽我所能。我觉得我非常接近做对了,但就是无法将最后几件事放在一起。我也意识到这是一个不太好的编码,但我是这方面的初学者,希望能得到任何帮助。
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();
}
}
PrintWriter 有向您隐藏错误的恶习。我建议换成另一个 read/write class。请查看以下内容以获取有关 BufferedWriter 的信息。或者,如果您受困于 PrintWriter,请查看如何检查错误。
http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
请注意,您必须使用 BufferedReader 添加换行符,因为它没有 println 方法。有关差异的更多详细信息,请查看此 post.
Difference between java.io.PrintWriter and java.io.BufferedWriter?