NIO.2特性总结(一)灵活的Path

天涯浪子 提交于 2019-12-09 13:02:55

NIO.2More New I/O APIs for the Java Platform

技术发展实在太快,NIO都没弄透,java7中的NIO2又要开始进入我们的程序,优化系统的性能了。最近在看《PRO Java 7 NIO.2CSDN上有下载,不用积分,好像只有英文版的,顺便做点儿笔记。

我先整体介绍下java7在NIO上做的一些改动。其实我找了官网的更新文件,但上面并没有说的很清楚,我就从书上给出的目录大致列一下,这里只说NIO的变化,其他的变化,我记得有本书叫java7新特性吧,以前InfoQ上看过。

NIO.2主要还是在NIO.1的基础上对文件系统和socket操作功能进行了增强。首先在增强对文件系统操作部分,增加了java.nio.file类,这部分在API内这样描述Defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems。这里有我今天要说的主管路径的Path类,有连接文件系统的FileSystem类等,与之相关的还有java.nio.file.attribute包下的内容。这里有对文件系统view的描述和操作。这部分可以归结为:

Path Operations

File Metadata

Manager Symbolic and Hard links

Deal with Files and Directories

Recursive File Operations

New API working for random access file

而在网络方面,我只想提java7对异步IO的操作,Asynchronous Channel。这部分是NIO.1非阻塞IO后最大的更新。如果你看过我对mina源码的解读,你应该还记得mina对异步的操作是采用future来实现的,这里更底层的实现出来了。

Path对象主要做两类事情,第一类叫syntactic operations,第二类operations over files referenced by paths。第一类就是对路径的操作,这类操作往往都在内存中进行,不需要直接接入文件系统。第二类操作,为文件操作提供一个引用,这个可能和之间我们写的URL比较相似。下面我们具体看写例子就能明白了。

The Path class is an upgraded version of the well-known java.io.File class, but the File class has kept a few specific operations, so it is not deprecated and cannot be considered obsolete. Moreover, starting with Java 7, both classes are available, which means programmers can mix their powers to obtain the best of I/O APIs. 这是不是意味着不久的将来File将会被取代,我们先看段代码:

/**
	 * out put: 1.txt file name:1.txt name count:2 parent:D:\nio2 root:D:\
	 */
	public static void testGetFile() {
		/** old method */
		File file1 = new File("D:\\nio2\\1.txt");
		System.out.println(file1.getName());

		/** nio2 */
		Path file = Paths.get("D:\\nio2\\1.txt");
		System.out.println("file name:" + file.getFileName());
		System.out.println("name count:" + file.getNameCount());
		System.out.println("parent:" + file.getParent() + " root:"
				+ file.getRoot());
		
		file1=file.toFile();
		System.out.println(file1.exists());
	}

我们看下JDKFile的描述:An abstract representation of file and directory pathnames. User interfaces and operating systems use system-dependent pathname strings to name files and directoriesFile类其实名字有点儿误导人,它并不是指真正的文件,它的本质是用一种依赖系统的路径名来描述文件或者目录。这个通俗点儿讲不就是path么。我们再看看两者的API的区别:

看着好像File类强大多了,但是你自己看看File类里的方法,好多还真的是对Path的操作,真正的创建文件,写入都是通过IO的。而反观Path类,确实简单了很多,更多的实现让其子类去实现了,在Path类里提供了toFile方法,将Path对象转成File对象。不过Path对象确实更关注的是路径,具体我们往下看。

关于Path的定义后好多种,这里只是列了其中的几种,具体可看《PRO Java 7 NIO.2》 :

public static void definePath(){
		/**Define an Absolute Path*/
		Path path = Paths.get("C:/rafaelnadal/tournaments/2009/BNP.txt");
		Path path = Paths.get("C:/rafaelnadal/tournaments/2009", "BNP.txt");
		Path path = Paths.get("C:", "rafaelnadal/tournaments/2009", "BNP.txt");
		Path path = Paths.get("C:", "rafaelnadal", "tournaments", "2009", "BNP.txt");
		/**Define a Path Relative to the File Store Root*/
		Path path = Paths.get("/rafaelnadal/tournaments/2009/BNP.txt");
		Path path = Paths.get("/rafaelnadal","tournaments/2009/BNP.txt");
		/**Define a Path Relative to the Working Folder*/
		Path path = Paths.get("rafaelnadal/tournaments/2009/BNP.txt");
		Path path = Paths.get("rafaelnadal","tournaments/2009/BNP.txt");
	}

另外根据上面的API截图,你能利用Path做这么一些但不局限于这么一些事:

Get information from path

Converting a path(其实就是把pathStringURL,把相对变绝对路径等等)

Combining two paths

Comparing two paths

Iterate over the Name elements of a path(就是把一个复杂的目录遍历成一级一级的)

l  ……

文件这块就简单点儿的介绍了,我还是想把重点放在异步NIO上,当然了最近主要要写的还是kafka的东西,《分布式环境搭建》别错过。这个就当做个笔记。每天都要接触好多东西,记不住了。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!