Delete file (photo) from folder when performing update

Asked

Viewed 260 times

0

I’m having trouble with a code. I am creating a form with a profile photo, but when you make a photo change in EDIT, I would like the photo to be deleted from the images folder. Today I can only delete and change the photo, but the old one remains. The code below is what I use to delete a record from my list:

public void apagar(UsuarioModel selcionado){
   if (selecionado != null){
      try{
          if (new UsuarioDao().apagar(selecionado)){
              lista.remove(selcionado);
              }

      }catch(Exception e){
          System.out.println(e.getMessage());
      }
  }

}

2 answers

0

You need the file path before anything else a look at the code below that will be clear to you

String fileName = "C:\images\olamundo.jpg";
removerArquivo(fileName);
public void removeArquivo (String fileName) 
{
    File file = new File(fileName);
    file.delete();
}

hope I’ve helped.

0

If you have the image or file path just do:

 File imagem = new File("path/da/imagem");
 imagem.delete();

.delete() => boolean return. I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.