File setLastModified() method in Java with Examples
The setLastModified() method is a part of File class.The function sets the last modified time of the file or directory. The function sets the last modified value of the file in milliseconds.
Function Signature:
public boolean setLastModified(long time)
Function Syntax:
file.setLastModified(time)
Parameters: This function accepts a long value as parameter which represents the new last modified time.
Return value: The function returns a boolean value which states whether the new last modified time is set or not.
Exception; This method throws following exceptions:
- SecurityException if the function is not allowed write access to the file
- IllegalArgumentException if the argument is negetive
Below programs will illustrate the use of the setLastModified() function:
Example 1: We will try to change the last modified time of a existing file in f: directory.
// Java program to demonstrate the // use of setLastModified() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("f:\\program.txt"); // The new last modified time long time = 100000000; // Check if the last modified time // can be set to new value if (f.setLastModified(time)) { // Display that the last modified time // is set as the function returned true System.out.println("Last modified time is set"); } else { // Display that the last modified time // cannot be set as the function returned false System.out.println("Last modified time cannot be set"); } } catch (Exception e) { System.err.println(e.getMessage()); } } } |
Output:
Last modified time is set
Example 2: We will try to change the last modified time of a non existing file in f: directory.
// Java program to demonstrate the // use of setLastModified() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("f:\\program1.txt"); // The new last modified time long time = 100000000; // Check if the last modified time // can be set to new value if (f.setLastModified(time)) { // Display that the last modified time // is set as the function returned true System.out.println("Last modified " + "time is set"); } else { // Display that the last modified time // cannot be set as // the function returned false System.out.println("Last modified time" + " cannot be set"); } } catch (Exception e) { System.err.println(e.getMessage()); } } } |
Output:
Last modified time cannot be set
The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file
Recommended Posts:
- File getCanonicalFile() method in Java with Examples
- File getTotalSpace() method in Java with examples
- File getUsableSpace() method in java with examples
- File list() method in Java with Examples
- File getFreeSpace() method in Java with examples
- File getParent() method in Java with Examples
- File delete() method in Java with Examples
- File setWritable() method in Java with Examples
- File createTempFile() method in Java with Examples
- File getPath() method in Java with Examples
- File listRoots() method in Java with examples
- File renameTo() method in Java with examples
- File getParentFile() method in Java with Examples
- File mkdir() method in Java with examples
- File setReadOnly() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



