您的位置:首页 > 编程语言 > Java开发

Java-1.7新特性之文件路径

2016-07-09 21:43 435 查看
Java7中对文件管理提供了大量的新API,这些新的接口可以使我们操纵文件以及文件夹更加方便。它们大多位于java.nio.file包下。

java.nio.file.Paths 包含了用于创建Path对象的静态方法

java.nio.file.Path 包含了大量用于操纵文件路径的方法

java.nio.file.FileSystems 用于访问文件系统的类

java.nio.file.FileSystem 代表了一种文件系统,例如Unix下的根目录为 / ,而Windows下则为C盘

java.nio.file.FileStore 代表了真正的存储设备,提供了设备的详尽信息

java.nio.file.attribute.FileStoreAttributeView 提供了访问文件的信息

一个路径可以确定一个文件或者文件夹的具体位置。Java7中用Path对象来实现对文件或者文件夹的操作。获得一个Path对象的方法有很多,主要有以下两种:

使用FileSystem对象的getPath方法

使用Path对象的get方法

我们先来看看getPath方法,示例代码如下。

Java代码



public static void main(String[] args) {

Path path = FileSystems.getDefault().getPath(”/Home/projects/node.txt”);
System.out.println();
System.out.println(”toString: ” + path.toString());
System.out.printf(”getFileName: %s\n”, path.getFileName());
System.out.printf(”getRoot: %s\n”, path.getRoot());
System.out.printf(”getNameCount: %d\n”, path.getNameCount());

for (int index = 0; index < path.getNameCount(); index++) {
System.out.printf(”getName(%d): %s\n”, index, path.getName(index));
}

System.out.printf(”subpath(0,2): %s\n”, path.subpath(0, 2));
System.out.printf(”getParent: %s\n”, path.getParent());
System.out.println(path.isAbsolute());

try {
path = Paths.get(”Home”, “projects”, “users.txt”);
System.out.printf(”Absolute path: %s”, path.toAbsolutePath());
} catch (InvalidPathException ex) {
System.out.printf(”Bad path: [%s] at position %s”, ex.getInput(), ex.getIndex());
}
}
public static void main(String[] args) {

Path path = FileSystems.getDefault().getPath("/Home/projects/node.txt");
System.out.println();
System.out.println("toString: " + path.toString());
System.out.printf("getFileName: %s\n", path.getFileName());
System.out.printf("getRoot: %s\n", path.getRoot());
System.out.printf("getNameCount: %d\n", path.getNameCount());

for (int index = 0; index < path.getNameCount(); index++) {
System.out.printf("getName(%d): %s\n", index, path.getName(index));
}

System.out.printf("subpath(0,2): %s\n", path.subpath(0, 2));
System.out.printf("getParent: %s\n", path.getParent());
System.out.println(path.isAbsolute());

try {
path = Paths.get("Home", "projects", "users.txt");
System.out.printf("Absolute path: %s", path.toAbsolutePath());
} catch (InvalidPathException ex) {
System.out.printf("Bad path: [%s] at position %s", ex.getInput(), ex.getIndex());
}
}


示例中FileSystems的getDefault方法,会由JVM返回一个代表了当前文件系统的FileSystem对象,我们可以通过FileSystem来获得Path对象。一个Path可以由多个子Path组成,子Path可以可通用过subpath方法来获得。

使用Paths类的get方法同样可以获得一个Path对象,如果你查看JDK的源码你会发现,它的实现方式和用FileSystem是一样的。

Java代码



public static Path get(String first, String… more) {
return FileSystems.getDefault().getPath(first, more);
}
public static Path get(String first, String... more) {
return FileSystems.getDefault().getPath(first, more);
}


如果我们使用的系统中有大量Java7之前对文件操作的代码,例如使用了很多File类的话,我们可以通过Java7中对File类新增的toPath方法来将一个File转换成Path。

Java代码



public static void main(String[] args) {
try {
Path path = Paths.get(new URI(“file:///C:/home/docs/users.txt”));
File file = new File(“C:\\home\\docs\\users.txt”);
Path toPath = file.toPath();
System.out.println(toPath.equals(path));
} catch (URISyntaxException e) {
System.out.println(”Bad URI”);
}
}
public static void main(String[] args) {
try {
Path path = Paths.get(new URI("file:///C:/home/docs/users.txt"));
File file = new File("C:\\home\\docs\\users.txt");
Path toPath = file.toPath();
System.out.println(toPath.equals(path));
} catch (URISyntaxException e) {
System.out.println("Bad URI");
}
}


将一个相对路径转换成绝对路径,只需要调用Path类的toAbsolutePath方法。此外如果想要获得系统的路径分隔符,FileSystem也提供了getSeparator方法。

Java代码



public static void main(String[] args) {

String separator = FileSystems.getDefault().getSeparator();
System.out.println(”The separator is ” + separator);

try {
Path path = Paths.get(new URI(“file:///D:/Home/projects/node.txt”));
System.out.println(”subpath: ” + path.subpath(0, 3));
path = Paths.get(”/home”, “docs”, “users.txt”);
System.out.println(”Absolute path: ” + path.toAbsolutePath());
System.out.println(”URI: ” + path.toUri());
} catch (URISyntaxException ex) {
System.out.println(”Bad URI”);
} catch (InvalidPathException ex) {
System.out.println(”Bad path: [“ + ex.getInput() + “] at position ” + ex.getIndex());
}
}
public static void main(String[] args) {

String separator = FileSystems.getDefault().getSeparator();
System.out.println("The separator is " + separator);

try {
Path path = Paths.get(new URI("file:///D:/Home/projects/node.txt"));
System.out.println("subpath: " + path.subpath(0, 3));
path = Paths.get("/home", "docs", "users.txt");
System.out.println("Absolute path: " + path.toAbsolutePath());
System.out.println("URI: " + path.toUri());
} catch (URISyntaxException ex) {
System.out.println("Bad URI");
} catch (InvalidPathException ex) {
System.out.println("Bad path: [" + ex.getInput() + "] at position            " + ex.getIndex());
}
}


判断一个文件是否真实存在,在Files类中提供了exists方法,它接收一个Path对象作为参数。

Java代码



Path path = Paths.get(new URI(“file:///C:/home/docs/bogusfile.txt”));
System.out.println(”File exists: ” + Files.exists(path));
Path path = Paths.get(new URI("file:///C:/home/docs/bogusfile.txt"));
System.out.println("File exists: " + Files.exists(path));


当路径描述符中有”.”或者”..”字符时,我们可以通过调用Path的normalize方法将这些描述符转换成真正的路径。运行下面的代码,你会发现”.”都被删除掉了,因为它代表当前路径。”..”会用上一层路径代替。

Java代码



public static void main(String[] args) {

Path path = Paths.get(”D:/home/docs/../music/Space Machine A.mp3”);

System.out.println(”Absolute path: ” + path.toAbsolutePath());
System.out.println(”URI: ” + path.toUri());
System.out.println(”Normalized Path: ” + path.normalize());
System.out.println(”Normalized URI: ” + path.normalize().toUri());
System.out.println();

path = Paths.get(”D:/home/./music/ Robot Brain A.mp3”);

System.out.println(”Absolute path: ” + path.toAbsolutePath());
System.out.println(”URI: ” + path.toUri());
System.out.println(”Normalized Path: ” + path.normalize());
System.out.println(”Normalized URI: ” + path.normalize().toUri());
}
public static void main(String[] args) {

Path path = Paths.get("D:/home/docs/../music/Space Machine A.mp3");

System.out.println("Absolute path: " + path.toAbsolutePath());
System.out.println("URI: " + path.toUri());
System.out.println("Normalized Path: " + path.normalize());
System.out.println("Normalized URI: " + path.normalize().toUri());
System.out.println();

path = Paths.get("D:/home/./music/ Robot Brain A.mp3");

System.out.println("Absolute path: " + path.toAbsolutePath());
System.out.println("URI: " + path.toUri());
System.out.println("Normalized Path: " + path.normalize());
System.out.println("Normalized URI: " + path.normalize().toUri());
}


Path的toRealPath会返回一个真实代表一个文件的路径,如果所指定的文件不存在,NoSuchFileException异常会被抛出。toRealPath可以传入一个LinkOption.NOFOLLOW_LINKS参数来将结果以链接的路径显示。Files判断文件是否存在的方法也可以加入此参数。下面的例子中music文件夹下的users是docs文件下users文件的链接。

Java代码



public static void main(String[] args) {

Path path1 = null;
Path path2 = null;

path1 = Paths.get(”/home/docs/users.txt”);
path2 = Paths.get(”/home/music/users.txt”);

System.out.println(Files.isSymbolicLink(path1));
System.out.println(Files.isSymbolicLink(path2));

try {

Path path = Paths.get(”C:/home/./music/users.txt”);

System.out.println(”Normalized: ” + path.normalize());
System.out.println(”Absolute path: ” + path.toAbsolutePath());
System.out.println(”URI: ” + path.toUri());
System.out.println(”toRealPath (Do not follow links): ” + path.toRealPath(LinkOption.NOFOLLOW_LINKS));
System.out.println(”toRealPath: ” + path.toRealPath());

Path firstPath = Paths.get(”/home/music/users.txt”);
Path secondPath = Paths.get(”/docs/status.txt”);

System.out.println(”From firstPath to secondPath: ” + firstPath.relativize(secondPath));
System.out.println(”From secondPath to firstPath: ” + secondPath.relativize(firstPath));
System.out.println(”exists (Do not follow links): ” + Files.exists(firstPath, LinkOption.NOFOLLOW_LINKS));
System.out.println(”exists: ” + Files.exists(firstPath));
System.out.println(”notExists (Do not follow links): ” + Files.notExists(firstPath, LinkOption.NOFOLLOW_LINKS));
System.out.println(”notExists: ” + Files.notExists(firstPath));

} catch (IOException ex) {
Logger.getLogger(SymbolicLinkExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {

Path path1 = null;
Path path2 = null;

path1 = Paths.get("/home/docs/users.txt");
path2 = Paths.get("/home/music/users.txt");

System.out.println(Files.isSymbolicLink(path1));
System.out.println(Files.isSymbolicLink(path2));

try {

Path path = Paths.get("C:/home/./music/users.txt");

System.out.println("Normalized: " + path.normalize());
System.out.println("Absolute path: " + path.toAbsolutePath());
System.out.println("URI: " + path.toUri());
System.out.println("toRealPath (Do not follow links): " + path.toRealPath(LinkOption.NOFOLLOW_LINKS));
System.out.println("toRealPath: " + path.toRealPath());

Path firstPath = Paths.get("/home/music/users.txt");
Path secondPath = Paths.get("/docs/status.txt");

System.out.println("From firstPath to secondPath: " + firstPath.relativize(secondPath));
System.out.println("From secondPath to firstPath: " + secondPath.relativize(firstPath));
System.out.println("exists (Do not follow links): " + Files.exists(firstPath, LinkOption.NOFOLLOW_LINKS));
System.out.println("exists: " + Files.exists(firstPath));
System.out.println("notExists (Do not follow links): " + Files.notExists(firstPath, LinkOption.NOFOLLOW_LINKS));
System.out.println("notExists: " + Files.notExists(firstPath));

} catch (IOException ex) {
Logger.getLogger(SymbolicLinkExample.class.getName()).log(Level.SEVERE, null, ex);
}
}


Java7中新引用的这些类的确大大简化了我们进行文件操作,使得操作文件不再那么繁琐。今后我们还会对如何修改,复制以及删除文件进行进一步了解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: