File.delete() deleting target file only at random times

TrianglesMcGee :

I'm trying to delete a file in a folder and the folder itself but it only deletes the folder at random times. Why does it happen? If I keep mashing the delete button it eventually gets deleted.

private void deleteCourseButtonMouseClicked(java.awt.event.MouseEvent evt) {
    File dltcycle = new File(C.viewcoursedirectory);
    System.out.println(dltcycle.getAbsolutePath());
    String[] cycle = dltcycle.list();
    for (int i = 0; i < cycle.length; i++) {
        File dlt = new File(C.viewcoursedirectory + "\\" + cycle[i]);
        System.out.println(C.viewcoursedirectory + "\\" + cycle[i]);
        System.out.println(dlt.getAbsolutePath());
        dlt.delete();
    } 
    dltcycle.delete(); 
 }

Expected result is for the folder in the directory to be deleted consistently. Not at random times after mashing the delete button. I'm sorry if the code block looks ugly, I don't really know how to format it. I don't know why the "{" and "}" are not appearing in the code block :X.

minus :

The most common cause for such behavior is a leaked stream. If you, for example, write a file using a FileOutputStream and somehow you do not close it,it will be eventually closed when it's reference gets garbage collected.

Check (if you open the file with either input or output streams) if the stream is always closed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131047&siteId=1