0

I can't seem to get my project to delete a file, this is the current code I'm running:

                        if(!(user.profilePicturePath.equals("Images/defaultprofilepicture1.png"))) {
                        File oldPicture = new File(user.profilePicturePath);
                        oldPicture.delete();
                    }

I've checked to make sure the path is correct however the image still won't delete, and I'm not too sure why it is... Does anyone have any clue?

Luke
  • 11
  • 2
  • Try [`Files.delete()`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#delete(java.nio.file.Path)) and check if there's an exception. – shmosel Mar 18 '19 at 22:24
  • What path are you feeding to `user.profilePicturePath`? It may help if we see more of your code :) – AlexH Mar 18 '19 at 22:25
  • @shmosel getting a NoSuchFileException, `java.nio.file.NoSuchFileException: Images\username20ProfilePicture.jpg` However, the file is clearly there: https://gyazo.com/c63bf518db727ffc884f6b0a03adf0da – Luke Mar 18 '19 at 22:28
  • 3
    `new File(user.profilePicturePath).exists()` - I bet will return `false`, as you are passing it a relative path, which is going to be based on the current working directory of your app – MadProgrammer Mar 18 '19 at 22:29
  • The path passed in is `Images\username20ProfilePicture.jpg`. Is there a way to delete it with this path? – Luke Mar 18 '19 at 22:31
  • What is your project hierarchy? Where is the image relative to the class invoking the delete method? – Cardinal System Mar 18 '19 at 22:32
  • @CardinalSystem that was the problem, I added in a higher path name and it now works! Thank you c: – Luke Mar 18 '19 at 22:35

0 Answers0