gpt4 book ai didi

Java覆盖文件

转载 作者:行者123 更新时间:2023-12-01 13:47:24 25 4
gpt4 key购买 nike

我现在有一个问题。我创建了一个简单的宠物酒店应用程序,我想实现删除功能,以便用户可以删除现有的预订。

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

预订文件已存在其中包含此详细信息。

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

这是一个用户输入,因此当用户想要删除记录时,他们将输入例如“Zhi”作为名字,“Kai”作为姓氏,之后出现一个新文件夹 TempBooking.txt,其中其他两个预订详细信息仍然存在,但原始 BookingDetails.txt 文件夹也仍然存在,其中包含原始预订记录。我现在的问题是覆盖该文件的函数是什么,以便临时文件(TempBooking.txt)将更改为 BookingDetails.txt。

最佳答案

您可以删除 BookingDetails.txt 并将 TempBooking.txt 重命名为 BookingDetails.txt

TempBooking.txt 中的所有内容写入 BookingDetails.txt

关于Java覆盖文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20258558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com