[英]Java overwriting file
I have a problem now. 我现在有问题。 I created a simple pet hotel application and I wanted to implement a delete function so user can remove existing booking. 我创建了一个简单的宠物旅馆应用程序,并且想要实现一个删除功能,以便用户可以删除现有的预订。
//the main one.
System.out.println("---------------------------");
System.out.println("Welcome to the delete menu:");
System.out.println("---------------------------");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter owner's first name");
String firstName = scanner.nextLine();
System.out.println("Enter owner's last name");
String lastName = scanner.nextLine();
delete(firstName, lastName);
//the delete class
public static void delete(String firstName, String lastName) throws Exception
{
File inputFile = new File("BookingDetails.txt"); // Your file
File tempFile = new File("TempBooking.txt");// temp file
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
while((currentLine = reader.readLine()) != null)
{
if(currentLine.contains(firstName) && currentLine.contains(lastName))
continue;
writer.write(currentLine+"\n");
}
writer.close();
}
There's a booking file already existed Which contains this details. 已经有一个包含此详细信息的预订文件。
Abid Akmal 03/09/2013 0129928272 Checkup File 10 250.0 Dog
Zhi Kai 12/11/2013 1029918811 Grooming Pika 1 25.0 Rabbit
Vincent Che 12/03/2013 0129817711 Grooming Fleese 2 50.0 Dog
It's a user input so when user want to delete the record, they will input for example Zhi as first name and Kai as last name, after that a new folder TempBooking.txt appear with the other two booking details that still exist But the original BookingDetails.txt folder also is still there with the original booking record. 这是用户输入的内容,因此当用户要删除记录时,他们将输入“ Zhi”作为姓氏,“ Kai”作为姓氏,之后会出现一个新文件夹TempBooking.txt,其中还有其他两个预订详细信息,但是原始的BookingDetails .txt文件夹也仍然保留原始预订记录。 My problem now what's the function to overwrite the file so the temp file (TempBooking.txt) will change to BookingDetails.txt. 我的问题现在有什么功能覆盖文件,因此临时文件(TempBooking.txt)将更改为BookingDetails.txt。
either you delete BookingDetails.txt
and rename TempBooking.txt
to BookingDetails.txt
. 您可以删除BookingDetails.txt
并将TempBooking.txt
重命名为BookingDetails.txt
。
or 要么
write all your content from TempBooking.txt
to BookingDetails.txt
将所有内容从TempBooking.txt
写入BookingDetails.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.