Thursday, July 30, 2009

How can I delete files from within a C or JAVA application?

I am working with Portlet factory and every time there is a slight lockup it creates large heapdump files....all these files are titled with the prefix heapdump, and also have files called javacore....





I want to build a separate application that cleans these files away automatically, most likely to be cycled twice or thrice a day.......how do I go about doing this?





thanks!

How can I delete files from within a C or JAVA application?
The second utility method is delete( ), which deletes the disk file represented by the path


of the invoking File object. It is shown here:


boolean delete( )


You can also use delete( ) to delete a directory if the directory is empty. delete( ) returns


true if it deletes the file and false if the file cannot be removed.








import java.io.*;





public class Delete2 {





public static void main(String[] argv) {


for (int i=0; i%26lt;argv.length; i++)


delete(argv[i]);


}





public static void delete(String fileName) {


try {


// Construct a File object for the file to be deleted.


File target = new File(fileName);





if (!target.exists()) {


System.err.println("File " + fileName +


" not present to begin with!");


return;


}





// Quick, now, delete it immediately:


if (target.delete())


System.err.println("** Deleted " + fileName + " **");


else


System.err.println("Failed to delete " + fileName);


} catch (SecurityException e) {


System.err.println("Unable to delete " + fileName +


"(" + e.getMessage() + ")");


}


}


}
Reply:You dont need a C or a java program.A simple shell script will help.You can search by using find which ouputs the filenames and their path.YOu can filter out particular files for grepping in the o/p and then xargs rm -f with each of them.That does the trick.

imperial

No comments:

Post a Comment