0

I am able to upload a file using Struts2, but I want to drop the specified directory but I don't know how to delete a file.

Here is my code:

public String execute(){
  destPath = "/tmp/listfile";
  try{
    System.out.println("Src File name: " + myFile);
    System.out.println("Dst File name: " + myFileFileName);                 
    File destFile  = new File(destPath, myFileFileName);
    FileUtils.copyFile(myFile, destFile);
    fileList = ListFiles.ListAllFiles("/tmp/listfile");  
    return "listfiles";  
  }
  catch(IOException e)
  {
    e.printStackTrace();
    return "ERROR";
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
user
  • 101
  • 1
  • 1
  • 4

3 Answers3

1

If you use Apache Commons IO it's a one-liner:

FileUtils.deleteDirectory(dir);
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Ran Adler
  • 3,587
  • 30
  • 27
1

I think you want to move file instead of copy

public String execute() {
  String destPath = "/tmp/listfile";
  try {
    System.out.println("Src File name: " + myFile);
    System.out.println("Dst File name: " + myFileFileName);                 
    Path source = Paths.get(myFile.getAbsolutePath());
    Path target = Paths.get(destPath);
    Files.move(source, target.resolve(myFileFileName), REPLACE_EXISTING);
    fileList =ListFiles.ListAllFiles("/tmp/listfile");  
    return "listfiles";  
   } catch(IOException e) {
     e.printStackTrace();
     return "ERROR";
   }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
0
FileUtils.getFile(destFile).delete();
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42