Revision as of 15:20, 7 April 2007 by 70.83.182.253(talk)(New page: ==Java== Category:Java import java.util.File; import java.util.Time; import java.util.Date; public class FileModicationTimeTest { // if 0L is returned then the file does no...)
import java.util.File;
import java.util.Time;
import java.util.Date;
public class FileModicationTimeTest {
// if 0L is returned then the file does not exist
public static long getFileModificationTime(String filename)
{
// Get the last modified time
long modifiedTime = new File(filename).lastModified();
return modifiedTime;
}
public static boolean setFileModificationTimeToNow(String filename)
// Set the last modified time
long newModifiedTime = System.currentTimeMillis();
boolean success = new File(filename).setLastModified(newModifiedTime);
return success;
}
public static boolean setFileModificationTime(String filename, long timeInMs)
// Set the last modified time
boolean success = new File(filename).setLastModified(newModifiedTime);
return success;
}
public static void test(String type, String filename) {
long t = getFileModificationTime(filename);
System.out.println("The following " + type + " called " + filename +
(t == 0 ? " does not exists : " was modified at " + new Time(t).toString() )
);
System.out.println("The following " + type + " called " + filename +
(!setFileModificationTimeToNow(filename) ? " does not exists. " : " was modified to current time." )
);
System.out.println("The following " + type + " called " + filename +
(!setFileModificationTime(filename,t) ? " does not exists. " : " was modified to previous time." )
);
}
public static void main(String args[]) {
test("file", "output.txt");
test("file", File.seperator + "output.txt");
test("directory", "docs");
test("directory", File.seperator + "docs" + File.seperator);
}
}