AboutNeal Ziring Expertise Almost anything about the core Java platform, with an emphasis on networking. Not familiar with JDBC or the new Java graphics APIs. [Sun Certified Java Programmer, JDK 1.1]
Experience Certified Java Programmer for JDK 1.1 and 2.0.
Expert: Neal Ziring Date: 5/27/2008 Subject: deletion of folder
Question i am working on a project where i have to delete the folder after 7 days.can you please tell me a program(code in java) which can delete the folder when it is 7 days old.
Answer Swati,
Java provides a class that will do most of what you need:
java.io.File. It allows you to check the modification date
of a file, and it allows you to delete files.
For example, given a file path, here is some code that
tells you how "old" the file is in days.
public float fileAge(String path) throws IOException {
File myfile;
myfile = new File(path);
long mtime;
mtime = myfile.lastModified();
long now;
now = System.currentTimeMillis();
long age;
age = now - mtime;
return (age / (1000.0 * 60 * 60 * 24));
}
Now, if you want to delete a file, you need to create a File
object, and call its delete() method.
For more information, check the JDK documentation for the
java.io.File object.