Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Why won't my file delete in java?

public boolean delete(String strFile) {

File fileToDelete = new File("ids/"+strFile+".txt");

if (!fileToDelete.exists())

return true;

else {

boolean blnDeleted = fileToDelete.delete();

System.out.println(fileToDelete.toString()+" Deleted: "+blnDeleted);

return blnDeleted;

}

}

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Your syntax and logic is definitely correct. (As your code shows, check for the file's existence using the exists() method, and just delete using the delete() method.)

    The only reasons I can think of are:

    1) You don't have permission to delete the file. One common reason this could be is because you were reading or writing to the file and never closed the stream.

    For instance,

    File file = new File ("temp.txt");

    BufferedReader in = new BufferedReader(new FileReader(file));

    String str = in.readLine();

    file.delete(); // This delete fails, because the buffered reader was never closed properly

    // To fix this, need to close the reader (using in.close() ) before attempting to delete file

    2) If the file is a system file and/or someone else is using it, you won't be able to delete it. Operating systems have the ability to decide whether or not to allow you to delete files. For instance, when Windows Vista came out, Microsoft made the Program Files folder protected. This meant that without administrator privileges, you cannot delete files in Program Files. However, many applications and games written during the Windows XP era assumed that they could create and delete files in the Program Files directory at will. Thus, these applications and games all fail in Windows Vista/7, unless you run them "as administrator".

    3) You may have gotten the file name and path wrong. Does the file exists? Try adding a debug println statement to see if the .exists() returns true or false. Then you'll know if the delete is failing because the file doesn't exist or because the delete() call is failing.

    4) You cannot delete directories that contain files. (So you can only delete empty directories.)

  • ?
    Lv 4
    4 years ago

    enable your AV cope with and do away with the trojans. do no longer uninstall Java, it is not the priority. Java is a programming language and you like the java runtime environment put in for some web pages to paintings as meant.

  • 4 years ago

    enable your AV cope with and eliminate the trojans. do not uninstall Java, it incredibly is not the project. Java is a programming language and you desire the java runtime surroundings put in for some web content to artwork as meant.

  • 1 decade ago

    It could be that you don't have read/write access to that file. If so you can try changing the files permissions. Also I'm sure you're aware that java applets can't read or write to files. Sorry if this wasn't much help.

  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    Try using this method instead.

    It is part of the Path class and not the File class so you will have to change your program.

    Have fun.

Still have questions? Get your answers by asking now.