简体   繁体   中英

delete a file in java does not work

i put this code,which i got from the internet, in my java program but when i try to delete, the original file cannot be deleted and the temporary file cannot be renamed to the original file.The two files remains in the folder with its contents unchanged.

...

public class FilingDatabase {
    public static void main(String[]args)throws IOException{
         (new FilingDatabase()).run();
        FilingDatabase fd=new FilingDatabase();
        String word = null;
        fd.delete("person.txt",word);

       }

.

public  void run() throws IOException{
    File file=new File("person.txt");
    BufferedReader br=new BufferedReader(new FileReader(file));

    while((str=br.readLine())!=null)
        i++;

       System.out.print("\t\t\t\t\t\t***************WELCOME*****************");
       System.out.println();
       System.out.println("1. Add \n2. Edit \n3. Delete \n4. Exit");
       System.out.print("\nEnter option number: ");
       option=in.next();

        while(true){
            ...
            else if(option.charAt(0)=='3'){
               // FilingDatabase fd= new FilingDatabase();
                System.out.print("Enter word: ");
                word=in.next();
                //delete("person.txt",word);

            }        
           ...
       }
    }//end of fxn run()

....

public void delete(String file, String lineToRemove) throws IOException{

         try {
                     File inFile = new File(file);
                     if (!inFile.isFile()){
                     System.out.println("File does not exist");
                     return;
                      }
                      File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
                      BufferedReader br = new BufferedReader(new FileReader(file));
                      //Scanner br=new Scanner(file);
                      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

                      String line = null;

                      while ((line = br.readLine()) != null) {
                        if (!line.trim().equals(lineToRemove)) {

                          pw.println(line);
                          pw.flush();
                        }
                      }


                      pw.close();
                      br.close();

                      if (!inFile.delete()) {
                        System.out.println("Could not delete file");
                        return;
                      } 
                      if (!tempFile.renameTo(inFile))
                        System.out.println("Could not rename file");

            }catch (FileNotFoundException e) {
                e.printStackTrace();
                }
  }
}

I'm not sure where you're trying to delete, but on the last line of your main:

 fd.delete("person.txt",word); 

will not delete anything because Object.equals(null) should always return false . ( word is null .)

If you're trying to delete inside your loop:

 // FilingDatabase fd= new FilingDatabase();
 System.out.print("Enter word: ");
 word=in.next();
 //delete("person.txt",word);

It won't delete anything because the delete line is commented out.

I'm not sure what to tell you about deleting and renaming the files, because that works for me.

I'm not going to try to get my head around your code ... and what it is trying to do. (Have you heard of comments? Javadocs? Have you considered using them?)

However, I'd like to point out that both delete and rename can fail under a number of circumstances. In the delete case, these include the following:

  • the target file does not exist
  • the target file does exist but the application doesn't have permission to access the parent directory and/or delete the file
  • the target object is a directory not a file
  • (on some platforms) the target file is locked because this application or another one currently has it open.

In the case of rename , you have to consider the existence, permissions, etc of the file being renamed (and its parent directory) and the directory you are trying to move. And there's also the issue that rename doesn't work between different file systems.


It is unfortunate that these methods (on the File class) don't say why the delete or rename failed. (The methods on the new Java 7 Files class do ...) Even if they were able to do this, the diagnostics would be limited by what the OS syscalls report. In the case of Unix / Linux, this is pretty limited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM