`

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

    博客分类:
  • java
 
阅读更多

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

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

我们看下这个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等。

 

0
9
分享到:
评论

相关推荐

    优雅的操作文件:java.nio.file 库介绍.pdf

    所以本章,我们就来主要介绍 java.nio.file 中常用的类和模块,大致如下: Path 路径:Paths 模块和 Path 工具类介绍 Files 文件:File 和 FileSystems 工具类介绍 文件管理服务:WatchService 、PathMatcher 等等...

    FileWatcher:使用 Java7 NIO 的文件系统观察者服务。 任何文件创建、删除或修改的警报

    在这里使用 Java 7 API 创建了一个文件系统功能来观察文件系统的变化。 现在,我们可以观察创建、删除、修改等事件,并参与我们自己的操作。 java.nio.file 包提供了一个文件更改通知 API,称为 Watch Service API...

    Java JDK 7学习笔记(国内第一本Java 7,前期版本累计销量5万册)

     《Java JDK 7学习笔记》针对Java SE 7新功能全面改版,无论是章节架构或范例程序代码,都做了全面重新编写与翻新。  《Java JDK 7学习笔记》是作者多年来教学实践的经验总结,汇集了学员在教学过程中遇到的概念、...

    java7-fs-more:java.nio.file 的一组实用方法(递归复制删除等)

    这是什么这是一组用于操作 JSR 203 的所有内容的实用程序,即新的 java.nio.file API。 它需要 Java 7+。 除了 JRE 之外没有其他依赖项。版本当前版本是0.2.0 : dependencies { compile( group : " ...

    《Programming in Java Second Edition》高清完整英文PDF版

    Java 7 includes support for strings in switch statements, try-with-resources statement, improved multi-catch, binary numeric literals, numeric literals with underscores, new APIs in NIO like Path and...

    jCIFS Filesystem:将jCIFS包装为Java文件系统-开源

    您只需使用Paths.get(new URI(“ smb:// hostname / share / filename”))即可访问cifs / smb文件,并使用实用程序类java.nio.file操作返回的Path。文件。 因此,您可以使用一个API通过Java应用程序访问本地和...

    疯狂JAVA讲义

    第1章 Java概述 1 1.1 Java语言的发展简史 2 1.2 Java的竞争对手及各自优势 4 1.2.1 C#简介和优势 4 1.2.2 Ruby简介和优势 4 1.2.3 Python的简介和优势 5 1.3 Java程序运行机制 5 1.3.1 高级语言的运行机制 6...

    tomcat-7_API_帮助文档

    There are some Linux bugs reported against the NIO sendfile behavior, make sure you have a JDK that is up to date, or disable sendfile behavior in the Connector. 6427312: (fc) FileChannel....

    javacv-platform-1.3.3-src

    To learn how to use the API, since documentation currently lacks, please refer to the Sample Usage section below as well as the sample programs, including two for Android (FacePreview.java and Record...

    fs:文件系统实用程序库

    babashka.fs ... 大多数函数将字符串java.io.File或java.nio.file.Path作为输入,并返回java.nio.file.Path 。 胁迫到File或Path是可以做到用fs/file和fs/path 。 用法 ( require '[babashka.fs :as fs]) ( fs

    msgpack-json:用于转换msgpack &lt;=> json的Web API

    $ scala -e 'java.nio.file.Files.write(new java.io.File&#40;"msgpack_data1"&#41;.toPath, Array(0x81, 0xa3, 0x61, 0x62, 0x63, 0x92, 0xc2, 0x2a).map(_.toByte))' $ curl -X POST -d @msgpack_data1 ...

    Jetty中文手册

    Index of Generated Release Documents–API and XRef documentation for previous releases. 通用参考 Jetty体系结构(Architecture) Jetty Classloading Jetty JARS和依赖(Dependencies) 启动参数(Start ...

    Windouws 64 位Tomcat7.0.40 + 64位jdk1.7.0u21 绿色版.part1

    Windouws 64 位Tomcat7.0.40 + 64位jdk1.7.0u21 绿色版 part1 纯绿色,官方下载,绝对可用~ 欢迎评论吐槽~ 文件 PATH 列表 │ LICENSE │ NOTICE │ RELEASE-NOTES │ RUNNING.txt │ ├─bin │ bootstrap.jar │...

    apktool documentation

    Remove framework file $HOME/apktool/framework/1.apk due to internal API update (Android Marshmallow) v1.5.x -&gt; v2.0.0 Java 1.7 is required Update apktool to v2.0.0 aapt is now included inside ...

Global site tag (gtag.js) - Google Analytics