Hi,
i written a code for delete particular file which it was saved in some folder "temp" which it was shown in the below.


try{

    java.io.File objFile = new java.io.File("F:/temp/FirstPdf-022.pdf");

    objFile.delete();

 } catch(Exception e) {

}

In the above code, i delete the file "FirstPdf-022.pdf". But i need delete all the files dynamically. If any one know the code, pls let me know.


Hi,
i written a code for delete particular file which it was saved in some folder "temp" which it was shown in the below.


try{

    java.io.File objFile = new java.io.File("F:/temp/FirstPdf-022.pdf");

    objFile.delete();

 } catch(Exception e) {

}

In the above code, i delete the file "FirstPdf-022.pdf". But i need delete all the files dynamically. If any one know the code, pls let me know.

//...
File file = new File("F:/temp");
if ( !file.canRead() ) {
   throw new IOException("Cannot list temp directory");
}

for ( String directoryEntry : file.list() ) {
   File toDelete = new File ( file.getAbsolutePath().concat( File.pathSeparator ).concat( directoryEntry );
   if ( toDelete.canWrite() ) {
      toDelete.delete();
   }
}

file.close();
//...

i need delete all the files dynamically

Sounds like you need to get the names of all the files in the directory and delete them one by one. Read the comments in the above code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.