当 FileWriter 将输出保存到文件时,它会保存两次。这是我的代码:
import java.io.*;
import java.util.Scanner;
public class Room {
static Scanner keyboard = new Scanner(System.in);
static int numberOfRooms;
static String roomType;
static char airCon;
static char balcony;
public static void addRoom() {
System.out.println("How many rooms would you like to add at once?");
numberOfRooms = keyboard.nextInt();
System.out.println("Choose the type of room:\\nR for regular\\nD for deluxe\\nS for suite\\n-> ");
roomType = keyboard.next();
System.out.println("Does this room have air conditioning? [T/F]");
airCon = keyboard.next().charAt(0);
System.out.println("Does this room have a balcony? [T/F]");
balcony = keyboard.next().charAt(0);
System.out.println("Rooms have been added successfully to the system. To list them, press 2 in menu");
}
public static void roomsInit() {
File myObj = new File("listOfRooms.txt");
try {
PrintWriter out = new PrintWriter(new FileWriter("listOfRooms.txt", true));
for (int i = 1; i <= numberOfRooms; i++) {
out.println("Room " + "#" + i + " " + roomType + " " + airCon + " " + balcony);
}
out.close();
}
catch (IOException ex)
{
//
}
}
}
我试着在 out.close() 之前放出 out.flush()。那没有用,它不尊重我的用户输入(numberOfRooms 变量)。
我希望我的
numberOfRooms.txt
文件看起来像这样:
Room #1 r t t
Room #2 r t t
Room #3 r t t
Room #4 r t t
Room #5 r t t
但它最终看起来像这样:
Room #1 r t t
Room #2 r t t
Room #3 r t t
Room #4 r t t
Room #5 r t t
Room #1 r t t
Room #2 r t t
Room #3 r t t
Room #4 r t t
Room #5 r t t
您两次看到输出的原因是您调用了 roomsInit() 方法两次,它使用 FileWriter 写入同一个文件。第一次它写入用户输入的房间数,第二次它再次写入相同的房间数,而不要求用户输入。
要解决此问题,您应该确保只调用一次roomsInit(),并确保在调用该方法之前初始化numberOfRooms变量。
实现此目的的一种方法是将 numberOfRooms 的用户输入代码移动到 roomsInit(),并将其从 addRoom() 中删除。然后,您可以调用 addRoom() 获取用户输入,并将 numberOfRooms 的值传递给 roomsInit() 以写入文件。这是一个如何修改代码的示例:
import java.io.*;
import java.util.Scanner;
public class Room {
static Scanner keyboard = new Scanner(System.in);
static int numberOfRooms;
static String roomType;
static char airCon;
static char balcony;
public static void addRoom() {
System.out.println("Choose the type of room:\\nR for regular\\nD for deluxe\\nS for suite\\n-> ");
roomType = keyboard.next();
System.out.println("Does this room have air conditioning? [T/F]");
airCon = keyboard.next().charAt(0);
System.out.println("Does this room have a balcony? [T/F]");
balcony = keyboard.next().charAt(0);
System.out.println("Rooms have been added successfully to the system. To list them, press 2 in menu");
}
public static void roomsInit(int numberOfRooms) {
try {
PrintWriter out = new PrintWriter(new FileWriter("listOfRooms.txt", true));
for (int i = 1; i <= numberOfRooms; i++) {
out.println("Room " + "#" + i + " " + roomType + " " + airCon + " " + balcony);
}
out.close();
}
catch (IOException ex)
{
//
}
}
public static void main(String[] args) {
System.out.println("How many rooms would you like to add at once?");
numberOfRooms = keyboard.nextInt();
addRoom();
roomsInit(numberOfRooms);
}
}
在此修改版本中,main()方法首先询问用户要添加的房间数量,然后调用addRoom()获取每个房间的详细信息,最后调用roomsInit() numberOfRooms 变量将房间写入文件。
请注意,在此示例中,我已从 numberOfRooms 变量中删除了 static 修饰符,并将其作为参数传递给 roomsInit()。这是因为通常最好避免使用全局变量,并在方法之间显式传递数据。