Files' static methods operate on Instance of File.
Files |
Let's take a look at the methods which check file attributes.
OCPJP>cat FilesDemo.java
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.UserPrincipal;
public class FilesDemo {
public static void main(String args[]) {
Path path = Paths.get("/Users/homenetwork/OCPJP");
checkAttributes(path);
checkAttributes(Paths.get("/Users/homenetwork/OCPJP/nosuchFile"));
checkAttributes(Paths.get("FilesDemo.java"));
try {
boolean flag = Files.isSameFile(Paths.get("/Users/homenetwork/./OCPJP"), path);
System.out.println("isSameFile? " + flag);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void checkAttributes(Path path) {
System.out.println("============" + path + "===============");
//System.out.println(path.toAbsolutePath());
boolean flag = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
System.out.println(flag);
System.out.println(Files.exists(path));
System.out.println(Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS));
System.out.println(Files.isExecutable(path));
System.out.println(Files.isReadable(path));
System.out.println(Files.isWritable(path));
System.out.println(Files.isSymbolicLink(path));
System.out.println(Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS));
try {
flag = Files.isHidden(path);
System.out.println(flag);
long size = Files.size(path);
System.out.println(size);
FileTime fileTime = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS);
System.out.println(fileTime.toMillis());
UserPrincipal owner = Files.getOwner(path, LinkOption.NOFOLLOW_LINKS);
System.out.println(owner.getName());
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
System.out.println(attr.isDirectory());
System.out.println(attr.isSymbolicLink());
System.out.println(attr.isRegularFile());
System.out.println(attr.size());
System.out.println(attr.lastModifiedTime().toMillis());
System.out.println(attr.lastAccessTime().toMillis());
System.out.println(attr.creationTime().toMillis());
//System.out.println(attr.fileKey());
} catch (IOException e) {
e.printStackTrace();
}
}
}
OCPJP>
OCPJP>javac FilesDemo.java
OCPJP>java FilesDemo
============/Users/homenetwork/OCPJP===============
true
true
true
true
true
true
false
false
false
884
1505616503000
homenetwork
true
false
false
884
1505616503000
1505650562000
1483829166000
============/Users/homenetwork/OCPJP/nosuchFile===============
false
false
false
false
false
false
false
false
false
java.nio.file.NoSuchFileException: /Users/homenetwork/OCPJP/nosuchFile
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:144)
at java.nio.file.Files.readAttributes(Files.java:1737)
at java.nio.file.Files.size(Files.java:2332)
at FilesDemo.checkAttributes(FilesDemo.java:37)
at FilesDemo.main(FilesDemo.java:10)
============FilesDemo.java===============
true
true
false
false
true
true
false
true
false
2575
1505012573000
homenetwork
false
false
true
2575
1505012573000
1505650556000
1505012573000
isSameFile? true
OCPJP>
In the above example, whenever LinkOption.NOFOLLOW_LINKS is used, this boolean flag is optional. Pay attention to the methods that throws IOException such as NoSuchFileException.
- Files.isHidden()
- Files.size()
- Files.getLastModifiedTime()
- Files.getOwner()
- Files.readAttributes()
- Files.isSameFile()
Files class can also modify files. The following example demonstrate these methods.
OCPJP>cat FilesModifyDemo.java
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.time.Instant;
import java.io.*;
public class FilesModifyDemo {
public static void main(String[] args) {
Path path = Paths.get("/Users/homenetwork/OCPJP/testDir");
System.out.println("========Files.createDirctory and Files.createDirectories======");
try {
System.out.println(Files.deleteIfExists(path)); //could throw DirectoryNotEmptyException
path = Files.createDirectory(path);
System.out.println(path);
path = Files.createDirectories(Paths.get("/Users/homenetwork/OCPJP/missing/testDir1"));
System.out.println(path);
path = Files.createDirectory(Paths.get("/Users/homenetwork/OCPJP/missing1/testDir1")); //NoSuchFileException
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("========Files.newBufferedWriter======");
path = Paths.get("/Users/homenetwork/OCPJP/testDir/testFile.txt");
try (BufferedWriter writer =
Files.newBufferedWriter(path);) {
writer.write("first line.");
writer.newLine();
writer.write("second line.");
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("========Files.newBufferedReader======");
try (BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())) {
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e1) {
e1.printStackTrace();
}
Path source = Paths.get("/Users/homenetwork/OCPJP/testDir/source.txt");
Path target = Paths.get("/Users/homenetwork/OCPJP/testDir/target.txt");
System.out.println("========Files.move======");
try {
System.out.println(Files.move(path, source));
System.out.println(Files.move(path, source,
StandardCopyOption.REPLACE_EXISTING)); //NoSuchFileException
} catch (IOException e2) {
e2.printStackTrace();
}
System.out.println("========Files.copy======");
try {
System.out.println(Files.copy(source, target));
System.out.println(Files.copy(source, target,
StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING));
System.out.println(Files.copy(source, target)); //FileAlreadyExistsException
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("========Files.copy with InputStream/OutputStream======");
try (InputStream in = new FileInputStream(source.toFile());
OutputStream out = new FileOutputStream(target.toFile())) {
System.out.println(Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING));
System.out.println(Files.copy(source, out)); //don't support copyOption
System.out.println(Files.copy(in, target)); //FileAlreadyExistsException
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("======Files.delete and Files.deleteIfExists======");
try {
path = source;
Files.lines(path).forEach(System.out::println);
Files.delete(path);
System.out.println(Files.deleteIfExists(path));
path = target;
Files.lines(path).forEach(System.out::println);
Files.delete(path);
System.out.println(Files.deleteIfExists(path));
Files.delete(path); //NoSuchFileException
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("======Files.setLastModifiedTime and Files.setOwner======");
try {
Files.createFile(path);
FileTime time = FileTime.fromMillis(System.currentTimeMillis());
System.out.println(Files.setLastModifiedTime(path, time));
UserPrincipal owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("homenetwork");
System.out.println(Files.setOwner(path, owner));
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
BasicFileAttributeView view =
Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
try {
Files.createFile(path);
FileTime lastModifiedTime = view.readAttributes().lastAccessTime();
FileTime lastAccessTime = FileTime.from(Instant.EPOCH);
FileTime createTime = null; //no change
view.setTimes(lastModifiedTime, lastAccessTime, createTime);
System.out.println(view.readAttributes().lastAccessTime());
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
OCPJP>javac FilesModifyDemo.java
OCPJP>java FilesModifyDemo
========Files.createDirctory and Files.createDirectories======
true
/Users/homenetwork/OCPJP/testDir
/Users/homenetwork/OCPJP/missing/testDir1
java.nio.file.NoSuchFileException: /Users/homenetwork/OCPJP/missing1/testDir1
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
at java.nio.file.Files.createDirectory(Files.java:674)
at FilesModifyDemo.main(FilesModifyDemo.java:17)
========Files.newBufferedWriter======
========Files.newBufferedReader======
first line.
second line.
========Files.move======
/Users/homenetwork/OCPJP/testDir/source.txt
java.nio.file.NoSuchFileException: /Users/homenetwork/OCPJP/testDir/testFile.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixCopyFile.move(UnixCopyFile.java:409)
at sun.nio.fs.UnixFileSystemProvider.move(UnixFileSystemProvider.java:262)
at java.nio.file.Files.move(Files.java:1395)
at FilesModifyDemo.main(FilesModifyDemo.java:51)
========Files.copy======
/Users/homenetwork/OCPJP/testDir/target.txt
/Users/homenetwork/OCPJP/testDir/target.txt
java.nio.file.FileAlreadyExistsException: /Users/homenetwork/OCPJP/testDir/target.txt
at sun.nio.fs.UnixCopyFile.copy(UnixCopyFile.java:551)
at sun.nio.fs.UnixFileSystemProvider.copy(UnixFileSystemProvider.java:253)
at java.nio.file.Files.copy(Files.java:1274)
at FilesModifyDemo.main(FilesModifyDemo.java:64)
========Files.copy with InputStream/OutputStream======
25
25
java.nio.file.FileAlreadyExistsException: /Users/homenetwork/OCPJP/testDir/target.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:88)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
at java.nio.file.Files.newOutputStream(Files.java:216)
at java.nio.file.Files.copy(Files.java:3016)
at FilesModifyDemo.main(FilesModifyDemo.java:75)
======Files.delete and Files.deleteIfExists======
first line.
second line.
false
first line.
second line.
false
java.nio.file.NoSuchFileException: /Users/homenetwork/OCPJP/testDir/target.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:244)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1126)
at FilesModifyDemo.main(FilesModifyDemo.java:91)
======Files.setLastModifiedTime and Files.setOwner======
/Users/homenetwork/OCPJP/testDir/target.txt
/Users/homenetwork/OCPJP/testDir/target.txt
1970-01-01T00:00:00Z
OCPJP>
Notice all of these methods throws IOException and its subclass.