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

时间:2013-12-10 18:49:23

标签: java

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

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

0 个答案:

没有答案