Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking Last Modification of a File on the Server in Java
Last Modification of a File
Many computer programs rely on the critical aspect of accessing and modifying files on a server in today's digital age. Yet, in order to guarantee the utilization of the latest information, it is frequently imperative to determine the last modification time of a file. Several strategies exist in Java for confirming the last modification of a file on a server, all of which have their benefits and drawbacks.
Method 1: (Using file.lastModified)
Accessing various file properties in Java can be done using the File class, including checking the last modification of a server file. Through utilizing the lastModified() method, the last modification time can be easily accessed once a File object that represents the server file is created. Using the Date class, the time in milliseconds since the epoch can be converted to a more readable format with this method.
Example
// Java Program to get last modification time of the file
import java.io.File;
import java.text.SimpleDateFormat;
public class Sample_Article {
public static void main(String[] args) {
// path of the file
String fileName = "C:/Users/aashi/Desktop/File.txt";
File file = new File(fileName);
// getting the last modified time of the file in the
// raw format I.e long value of milliseconds
System.out.println("Before Format : "+ file.lastModified());
// getting the last modified time of the file in
// terms of time and date
SimpleDateFormat sdf
= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(
"The Date and Time at which the file was last modified is "
+ sdf.format(file.lastModified()));
}
}
Output
Before Format : 0 The Date and Time at which the file was last modified is 01/01/1970 00:00:00
Method 2: (Using BasicFile Attributes)
Another method for checking the last modification of a file on a server in Java is using basic file attributes. By Using java.nio.*, one can conveniently view both a file's metadata and various attributes, such as creation time, last access time, and last modified time.
Example
// Java Program to get last modification time of the file
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
public class Sample_Article {
public static void main(String args[]) {
// storing the path of the file in the string
String fileName = "C:/Users/aashi/Desktop/File.txt";
// used exception handling in order to catch the
// exception
// which may occur if there is no such file is
// present at specified location
// or the path of the file provided by the user is
// incorrect
try {
// getting the path of the file
Path file = Paths.get(fileName);
// reading the attributes of the file
BasicFileAttributes attr = Files.readAttributes(
file, BasicFileAttributes.class);
// getting the last modification time of the
// file
System.out.println(
"lastModifiedTime of File Is : "+ attr.lastModifiedTime());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output
LastModifiedTime of a file is: 2023-05-02 T: 13:25:20
Method 3: (Using URL Class)
The third method for checking the last modification of a file on a server in Java is through the use of the URL Class. This provides a method for retrieving the last modification time of an uploaded file and retrieving the file's timestamp. This method requires authentication to access the server.
Example
// Java Program to get last modification time of the file
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Date;
public class Sample_Article {
public static void main(String[] args) throws Exception{
// resource url
URL u = new URL(
"https://www.waytoclass.org/file-handling-java-using-filewriter-filereader/");
URLConnection uc = u.openConnection();
uc.setUseCaches(false);
// getting the last modified time of the file
// uploaded on the server
long timestamp = uc.getLastModified();
System.out.println("The last modification time of java.bmp is:"
+ timestamp);
}
}
Output
The last modification time of java.bmp is:0
Conclusion
There are various approaches, each with advantages and disadvantages, for determining when a file on a server was last modified in Java. The BasicFile Attributes function enables remote access to the retrieval of file information, while the file class offers a basic and easy to use approach for retrieving file properties. Although it requires authentication, the URL Class method offers a more secure way of viewing and editing files on a remote server. The strategy used will rely on the program's particular requirements and available resources.