Java Reference
In-Depth Information
long size(Path)
•
boolean isHidden(Path path)
•
boolean isRegularFile(Path path, LinkOption... options)
•
boolean isDirectory(Path path, LinkOption... options)
•
boolean isSymbolicLink(Path path)
•
FileTime getLastModifiedTime(Path path, LinkOption... options)
•
Probing the Content Type of a File
You can use the
Files.probeContentType(Path path)
method to probe the content type of a file. The method returns
the content type in the string form of the value of a Multipurpose Internet Mail Extension (MIME) content type. If the
content type of a file cannot be determined, it returns
null
.
Listing 10-6 shows how to probe the content type of a file. You may get a different output when you run this
program. The program uses the file path
C:\poems\luci1.txt
. Please change this path to the path of the file whose
content type you want to know.
Listing 10-6.
Probing the Content Type of a File
// ProbeFileContent.java
package com.jdojo.nio2;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class ProbeFileContent {
public static void main(String[] args) {
Path p = Paths.get("C:\\poems\\luci1.txt");
try {
String contentType = Files.probeContentType(p);
System.out.format("Content type of %s is %s%n", p, contentType);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Content type of C:\poems\luci1.txt is text/plain