简体   繁体   中英

The .delete() method in java is not working

Files inside the (Tracks)directory was not deleted. the method deletes the wav files stored in the directory

public boolean deleteTrack(response)  {
    ListIterator<Track> trackListIterator = this.trackList.listIterator();

    //tracklist is the linked list on which I'm using list iterator. I'm storing song which is a object inside it. this object has a fn() that returns content root path not absolute path.

    String path = "";
    while (trackListIterator.hasNext()) {
     //RESPONSE == PARAMETER
        if (trackListIterator.next().getTrackName().equals(response)) {
            trackListIterator.previous();
            path = trackListIterator.next().getTrackPath();//this is the fn() that 
             returns content root path example(src/Exploit/org/Trackstore/Music/Action Movie Music-FesliyanStudios.wav).
            break;
        }
    }
   
    File file = new File(path);
 //here I'm taking absolute path for deleting actual wav file from the computer.
    File toBeDeleted = new File(file.getAbsolutePath());
    return toBeDeleted.delete();// returns false everytime.
}

file structure

The old API has many issues. For example, most methods return a boolean to indicate the result which is stupid and unjavalike - fortunately, there is a new API that fixes these issues.

Use it and you'll know WHY it failed. If that's too much effort, well, there isn't much to say. It didn't delete. No idea why, and there's no way to ask that API about why.

The new API lives in the java.nio.file package.

Replace this:

File f = new File("path/to/file");
if (!f.delete()) { ... it didn't work ... }

with:

Path p = Paths.get("path/to/file");
try {
    Files.delete(p);
} catch (IOException e) {
    // the exception will explain everything there is to be said about why it did not work!
}

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