We must read other File operations like - create, copy, move, rename, delete, exist?, append, hide/unHide, compare, size, Read only and writable.
In this post we’ll write
- Program 1 to Find creation, last modification and last accessed date of file in date, month and year in java7 using java.nio.file
- Program 2 to Find creationdate of file in date, month and year in java7 using java.nio.file
Program 1 to Find creation, last modification and last accessed date of file in date, month and year in java7 using java.nio.file >
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
/** JavaMadeSoEasy.com */
public class CreationDateInJava7 {
public static void main(String... args) throws IOException {
String fileName = "c:/myFile.txt";
// create java.nio.file.Path
Path path = Paths.get(fileName);
// create BasicFileAttributeView and BasicFileAttributes
BasicFileAttributeView basicFileAttributeView = Files
.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicFileAttributes = basicFileAttributeView
.readAttributes();
//Now, let's find creation, last modification and last accessed date
System.out.println(fileName + " was created on : "
+ basicFileAttributes.creationTime());
System.out.println(fileName + " was last modified on : "
+ basicFileAttributes.lastModifiedTime());
System.out.println(fileName + " was last accessed on : "
+ basicFileAttributes.lastAccessTime());
}
}
/* OUTPUT
c:/myFile.txt was created on : 2015-07-24T18:11:32.003828Z
c:/myFile.txt was last modified on : 2015-07-28T09:54:28.896337Z
c:/myFile.txt was last accessed on : 2015-07-28T18:11:32.003828Z
*/
|
Program 2 to Find creationdate of file in date, month and year in java7 using java.nio.file >
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/** JavaMadeSoEasy.com */
public class CreationDateInJava7 {
public static void main(String... args) throws IOException {
String fileName = "c:/myFile.txt";
// create java.nio.file.Path
Path path = Paths.get(fileName);
// create BasicFileAttributeView and BasicFileAttributes
BasicFileAttributeView basicFileAttributeView = Files
.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicFileAttributes = basicFileAttributeView
.readAttributes();
// Now, let's find creation date in date, month and year
Date creationDate = new Date(basicFileAttributes.creationTime().to(
TimeUnit.MILLISECONDS));
System.out.println(fileName + " creation date " + "\n date : "
+ creationDate.getDate() + "\n month : "
+ (creationDate.getMonth() + 1) + "\n year : "
+ (creationDate.getYear() + 1900));
}
}
/* OUTPUT
c:/myFile.txt creation date
date : 24
month : 7
year : 2015
*/
|
RELATED LINKS>
Create File using createNewFile() method in java file IO
Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)
Program to Move a file - in java file IO
Program to Rename a file - in java file IO
Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO
Traverse a Directory (all sub-directories and files) in 2 ways
Labels:
Core Java
File IO/File handling