java 7 NIO2(1) Path类相关新API

java 7 新的NIO API新增了FileSystem和FileStore相关API,最主要的是Path.java类,主要是我们常见的文件的一些相关操作,这个Path是NIO IO操作的基础,我们可以定义一个文件路径,对路径的相关操作等,

我们先看下java7 NIO 2新增的API:

java 7 NIO2(1) Path类相关新API_第1张图片

我们看下这个API使用的示例:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;

//NIO 2 file APi filesystem filestore
public class NIO2File {

	public static void main(String[] args) {
		try {
			// FileSystems.getDefault().getPath(first, more)
			Path path = Paths.get(System.getProperty("user.home"), "www",
					"pyweb.settings");
			Path real_path = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
			System.out.println("Path to real path: " + real_path);

			System.out.println("Number of name elements in path: "
					+ path.getNameCount());
			for (int i = 0; i < path.getNameCount(); i++) {
				System.out.println("Name element " + i + " is: "
						+ path.getName(i));
			}
			System.out.println("Subpath (0,3): " + path.subpath(0, 3));

			File path_to_file = path.toFile();
			Path file_to_path = path_to_file.toPath();
			System.out.println("Path to file name: " + path_to_file.getName());
			System.out.println("File to path: " + file_to_path.toString());

			Path base = Paths.get(System.getProperty("user.home"), "www",
					"pyweb.settings");
			// resolve AEGON.txt file
			Path path1 = base.resolve("django.wsgi");
			System.out.println(path1.toString());

			Path path2 = base.resolveSibling(".bashrc");
			System.out.println(path2.toString());

			Path path01_to_path02 = path1.relativize(path2);
			System.out.println(path01_to_path02);

			try {
				boolean check = Files.isSameFile(path1.getParent(),
						path2.getParent());
				if (check) {
					System.out.println("The paths locate the same file!"); // true
				} else {
					System.out
							.println("The paths does not locate the same file!");
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}

			boolean sw = path1.startsWith("/rafaelnadal/tournaments");
			boolean ew = path1.endsWith("django.wsgi");
			System.out.println(sw);
			System.out.println(ew);

			for (Path name : path1) {
				System.out.println(name);
			}

		} catch (NoSuchFileException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		}

	}
}

 输出:

Path to real path: /home/weijianzhongwj/www/pyweb.settings
Number of name elements in path: 4
Name element 0 is: home
Name element 1 is: weijianzhongwj
Name element 2 is: www
Name element 3 is: pyweb.settings
Subpath (0,3): home/weijianzhongwj/www
Path to file name: pyweb.settings
File to path: /home/weijianzhongwj/www/pyweb.settings
/home/weijianzhongwj/www/pyweb.settings/django.wsgi
/home/weijianzhongwj/www/.bashrc
../../.bashrc
The paths does not locate the same file!
false
true
home
weijianzhongwj
www
pyweb.settings
django.wsgi
 API很类似linux上的文件操作命令,新增的unix fifilesystem等。

 

java 7 NIO2(1) Path类相关新API_第2张图片

你可能感兴趣的:(java,7)