Java 如何使用原始文件名重命名文件并删除原始文件?

Java 如何使用原始文件名重命名文件并删除原始文件?,java,Java,我遇到的问题是,每次运行程序时,它都会返回false,因为它将新文件的名称更改为原始文件,并删除原始文件本身。如果有人发布一些代码来解决这个问题,我将不胜感激。我突出显示了上面输出反馈的代码。删除原始文件,然后重命名新文件。(按此顺序)您能提供一个基于我的代码的示例吗?现在您正在将file1重命名为file,然后删除file,什么也不留下。首先删除文件,然后将文件1重命名为文件。使用renameTo()覆盖文件本质上依赖于平台,通常被认为是不好的做法。首先删除文件,然后移动文件1。请阅读以下内容

我遇到的问题是,每次运行程序时,它都会返回false,因为它将新文件的名称更改为原始文件,并删除原始文件本身。如果有人发布一些代码来解决这个问题,我将不胜感激。我突出显示了上面输出反馈的代码。

删除原始文件,然后重命名新文件。(按此顺序)您能提供一个基于我的代码的示例吗?现在您正在将file1重命名为file,然后删除file,什么也不留下。首先删除文件,然后将文件1重命名为文件。使用
renameTo()覆盖文件本质上依赖于平台,通常被认为是不好的做法。首先删除
文件
,然后移动
文件1
。请阅读以下内容:
import java.io.*;
import java.util.*;
import java.lang.*;

public class FileWritingApp{
    public static void main(String[] args) {
        String inputFilename = "marks.txt"; // It expects to find this file in the same folder as the source code
        String outputFilename = "fakemarks.txt"; // The program will create this file
        PrintWriter outFile;
        String name,choice;

        int mark1,mark2;
        boolean flag=false;
        Scanner input = new Scanner(System.in);
        System.out.println("Do you want to add or find a student?");

        try {
            outFile = new PrintWriter(new FileWriter(outputFilename),true); // create a new file object to write to
            File file = new File(inputFilename); // create a file object to read from
            File file1 = new File(outputFilename);
            Scanner scanner = new Scanner(file); // A scanner object which will read the data from the file passed in.
            choice= input.nextLine();

            switch(choice){
                case "f":
                    System.out.println("Enter a name:");
                    name=input.nextLine();

                    while (scanner.hasNextLine()) { // This will loop until there are no more lines to read
                        String line = scanner.nextLine();
                        if(line.contains(name)){
                            System.out.println("Enter the first mark set:");
                            mark1=input.nextInt();

                            System.out.println("Enter the second mark set:");
                            mark2=input.nextInt();

                            line=name+", " + mark1 +", "+ mark2;
                            outFile.println(line);
                            flag=true;
                        } else {
                            outFile.println(line);
                        }
                    }

                    if(flag==false){
                        System.out.println('"'+name+'"'+" wasn't found");
                    }

                    break;

                case "a":
                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        outFile.println(line);
                    }
                    System.out.println("Enter a name:");
                    name=input.nextLine();

                    System.out.println("Enter the first mark set:");
                    mark1=input.nextInt();

                    System.out.println("Enter the second mark set:");
                    mark2=input.nextInt();

                    outFile.println(name+", " + mark1 +", "+ mark2);
                    break;
            } 
            ***scanner.close();
            outFile.close();
            if(file1.renameTo(file)){
                System.out.println("rename succesful");
            } else {
                System.out.println("rename unsuccesful");
            }
            if(file.delete()){
                System.out.println("delete succesful");
            } else {
                System.out.println("delete unsuccesful");
            }***    
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}