How To Boot The Bucket The End Modified Engagement Together With Fourth Dimension Of A File Or Directory Inwards Java

Sometimes before processing a file, you lot desire to banking concern gibe it's lastly modified appointment to avoid processing an erstwhile file. Though some programmers prefer to attach appointment inwards the file refer itself, I don't discovery it a cleaner approach. For example, suppose you lot are downloading closing prices of stocks together with processing at the commencement of the solar daytime together with loading into the database. In lodge to accidently procedure an erstwhile file, you lot tin flame banking concern gibe the lastly modified appointment before processing together with if it's inwards the acceptable range, you lot tin flame procedure the file. You tin flame acquire the lastly modified appointment of a file inwards Java yesteryear using java.io.File class. This is a flat which represents both file together with directory inwards Java. It contains a method called lastModified() which returns the lastly modified appointment of the file. This method returns a long millisecond epoch value, which you lot tin flame convert to to a greater extent than readable dd MM yyyy HH:mm:sss format yesteryear using the SimpleDateFormat flat of JDK.  In this article, I'll say you lot how to acquire the lastly modified appointment of file together with directory inwards Java.


The higher upward solution is to attain off all Java versions inwards hear i.e. it volition move inwards all Java version from JDK 1 to JDK 8, merely if you lot are writing novel code together with cause got access to much improved together with novel Java seven together with Java 8 API, you lot should purpose the  public static FileTime getLastModifiedTime(Path path, LinkOption... options) method which returns a file's lastly modified time.

The options array may live used to betoken how symbolic links are handled for the illustration that the file is a symbolic link. By default, symbolic links are followed together with the file attribute of the end target of the link is read. If the choice NOFOLLOW_LINKS is acquaint thus symbolic links are non followed. You tin flame every bit good acquire lastly modified fourth dimension yesteryear using readAttributes() method together with using the lastModifiedTime attribute.


Btw, you lot tin flame every bit good alter the lastly modified fourth dimension of a file yesteryear using the public static Path setLastModifiedTime(Path path, FileTime time) method. This i updates a file's lastly modified fourth dimension attribute. The file fourth dimension is converted to the epoch together with precision supported yesteryear the file system. Converting from finer to coarser granularities may number inwards precision loss.

The behaviour of this method when attempting to gear upward the lastly modified fourth dimension when it is non supported yesteryear the file organization or is exterior the attain supported yesteryear the underlying file shop is non defined. It may or non neglect yesteryear throwing an IOException.



How to discovery the lastly modified appointment of a file inwards Java

Before Java seven e.g. inwards JDK 1.6, You tin flame purpose the lastModified() method of File flat to acquire the lastly updated time. Just practise a File object yesteryear passing the path every bit String to File flat together with telephone telephone the lastly modified method every bit shown below:


File source = new File(".project"); long lastModified = source.lastModified();

This lastModified fourth dimension is the long millisecond value from epoch, 1st Jan 1970. You tin flame farther convert this into a java.util.Date object yesteryear next steps given here.

In JDK seven together with amongst novel file API, the java.nio.file, you lot tin flame purpose the Files flat to shout out upward the lastly modified fourth dimension of a file inwards Java every bit shown inwards the next example:

 Path location = Paths.get(".project");  FileTime lastModifiedTime = Files.getLastModifiedTime(location,                   LinkOption.NOFOLLOW_LINKS);

You tin flame farther convert the FileTime to long millisecond value yesteryear using the toMillis() method together with thus you lot tin flame practise the java.util.Date object from at that spot every bit shown earlier.  You tin flame every bit good read the Well-Grounded Java Developer  to acquire to a greater extent than most the novel file API of Java 7, along amongst several other tips for experienced Java programmers on concurrency together with polyglot programming e.g. using Scala together with Closure.


s lastly modified appointment to avoid processing an erstwhile file How to acquire the lastly modified appointment together with fourth dimension of a file or directory inwards Java



How to acquire the lastly modified appointment of a directory inwards Java

The steps to acquire the lastly updated fourth dimension for both file together with directory is same, every bit java.io.File represents both file together with directory. The entirely deviation is you lot overstep the path of the file instead of a file.

File settings = new File(".settings"); long lastChanged = settings.lastModified();


You tin flame every bit good farther read, Java SE 8 for Really Impatient By Cay S. Horstmann to acquire to a greater extent than most how to purpose novel File API of JDK seven along amongst Java 8 goodies.

s lastly modified appointment to avoid processing an erstwhile file How to acquire the lastly modified appointment together with fourth dimension of a file or directory inwards Java



Java Program to acquire the lastly modified appointment of file or directory

Here is our consummate Java plan to discovery the lastModifiedTime of a file or directory inwards Java. In this program, I cause got shown both erstwhile together with novel means of retrieving the lastly modified fourth dimension of a file inwards Java. Before JDK 7, you lot tin flame purpose the lastModified() method of File flat to shout out upward this value.

Post JDK 1.7 version i.e. inwards both Java seven together with 8, you lot tin flame purpose the Files.getLastModifiedTime() to shout out upward the lastly modified fourth dimension together with Files.setLastModifiedTime() to update this attribute of the file.  I every bit good propose reading a skilful Java majority which covers both JDK seven together with 8 to acquire to a greater extent than most the novel File API. It contains lots of utility methods to brand your day-to-day life simpler piece dealing amongst files together with directories inwards Java. You tin flame convey a hold off at Java SE8 for Programmers yesteryear Dietel together with Dietel

Here is a squeamish summary of diverse File utility methods from Java half-dozen to Java 7, you lot tin flame purpose this handy listing whenever you lot search for an equivalent method inwards novel file API.

s lastly modified appointment to avoid processing an erstwhile file How to acquire the lastly modified appointment together with fourth dimension of a file or directory inwards Java



How to discovery lastModifiedTime of File inwards Java
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;  /**  * Java Program to discovery the lastly modified appointment of a file  * together with directory inwards Java.   *   * @author WINDOWS 8  *  */ public class Pattern {    public static void main(String args[]) throws IOException{      // Example 1 - getting lastly modified appointment of a file inwards Java     System.out.println("Finding the lastly modified fourth dimension of file together with directory inwards Java half-dozen together with before");     File source = new File(".project");     long lastModified = source.lastModified();     String time = format(lastModified);     System.out.printf("file %s was lastly modified at %s %n", source.getName(),         time);      // Example two - how to acquire lastly modified appointment of a directory inwards Java     File settings = new File(".settings");     long lastChanged = settings.lastModified();     String lastUpdated = format(lastChanged);     System.out.printf("directory %s was lastly updated at %s %n",         settings.getName(), lastUpdated);          // Finding lastly modified fourth dimension of a file inwards Java seven together with 8     System.out.println("Finding the lastly modified fourth dimension of file together with directory inwards Java seven together with 8");     Path location = Paths.get(".project");     FileTime lastModifiedTime = Files.getLastModifiedTime(location, LinkOption.NOFOLLOW_LINKS);     String lastModifiedTimeAsString = format(lastModifiedTime.toMillis());     System.out.printf("file %s was lastly modified at %s %n", location,lastModifiedTimeAsString);        }    public static String format(long time) {     DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");     return sdf.format(new Date(time));   }  } Output Finding the last modified time of file and directory in Java 6 and before file .project was last modified at 04-06-2015 00:27:06  directory .settings was last updated at 04-06-2015 00:27:06  Finding the last modified time of file and directory in Java 7 and 8 file .project was last modified at 04-06-2015 00:27:06 


That's all about how to acquire the lastly modified appointment of a file or directory inwards Java. As I said, same flat java.io.File is used to stand upward for both file together with directory together with it every bit good provides a lastModified() method, which tin flame live used to acquire the fourth dimension when that detail file or directory was lastly modified. If you lot are using Java seven or Java 8 thus you lot tin flame every bit good purpose the java.nio.file, the novel File API to both shout out upward together with update the lastModifiedTime attribute of a file inwards Java. It provides Files flat to shout out upward diverse file attributes. I every bit good propose you lot read a skilful heart together with person Java majority which covers JDK seven or 8 e.g. Java: Influenza A virus subtype H5N1 Beginner's Guide yesteryear Herbert Schildt from Oracle itself, it covers Java 8 every bit well.



Other Java File together with Directory tutorials you lot may similar to explore 
  • How to delete a directory amongst files together with subdirectory inwards Java? (tutorial)
  • How to read Microsoft XLS file inwards Java? (example
  • Right means to Read Zip Files inwards Java (example
  • How to move amongst RandomAccessFile inwards Java? (demo
  • How to read/write text files inwards Java? (solution
  • Difference betwixt getPath(), getCannonicalPath() together with getAbsolutePath() inwards Java? (answer
  • How to read/write Properties files inwards Java? (example
  • How to brand hidden file inwards Java? (program
  • How to purpose Memory Mapped File inwards Java? (code
  • How to modify an Excel file inwards Java using Apache POI library (tutorial)
  • How to re-create File inwards Java? (solution
  • How to read File trouble yesteryear trouble inwards Java using BufferedReader? (example
  • How to banking concern gibe File Permission inwards Java? (program
  • How to alter File Permission inwards Java? (solution
  • How to practise File together with Directory inwards Java? (solution
  • How to banking concern gibe hidden file inwards Java? (solution
  • How to read File inwards i trouble inwards Java 8? (example

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

0 Response to "How To Boot The Bucket The End Modified Engagement Together With Fourth Dimension Of A File Or Directory Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel