Java NIO Path
Java NIO Path
The Java Path
interface is part of the Java NIO 2 update which Java NIO received in Java 6 and Java 7. The Java Path
interface was added to Java NIO in Java 7. The Path
interface is located in the java.nio.file
package, so the fully qualified name of the Java Path
interface is java.nio.file.Path
.
A Java Path
instance represents a path in the file system. A path can point to either a file or a directory. A path can be absolute or relative. An absolute path contains the full path from the root of the file system down to the file or directory it points to. A relative path contains the path to the file or directory relative to some other path. Relative paths may sound a bit confusing. Don't worry. I will explain relative paths in more detail later in this Java NIO Path tutorial.
Do not confuse a file system path with the path
environment variable in some operating systems. The java.nio.file.Path
interface has nothing to do with the path
environment variable.
Path能代表文件或目录
In many ways the java.nio.file.Path
interface is similar to the java.io.File
class, but there are some minor differences. In many cases though, you can replace the use of the File
class with use of the Path
interface.
NIO中的path有点像io中的File类
Creating a Path Instance
In order to use a java.nio.file.Path
instance you must create a Path
instance. You create a Path
instance using a static method in the Paths
class (java.nio.file.Paths
) named Paths.get()
. Here is a Java Paths.get()
example:
创造path实例用paths.get方法
import java.nio.file.Path;
import java.nio.file.Paths; public class PathExample { public static void main(String[] args) { Path path = Paths.get("c:\\data\\myfile.txt"); }
}
Notice the two import
statements at the top of the example. To use the Path
interface and the Paths
class we must first import them.
Second, notice the Paths.get("c:\\data\\myfile.txt")
method call. It is the call to the Paths.get()
method that creates the Path
instance. The Paths.get()
method is a factory method for Path
instances, in other words.
这个方法是一个工厂方法
Creating an Absolute Path
绝对路径创建path
Creating an absolute path is done by calling the Paths.get()
factory method with the absolute file as parameter. Here is an example of creating a Path
instance representing an absolute path:
Path path = Paths.get("c:\\data\\myfile.txt");
The absolute path is c:\data\myfile.txt
. The double \
characters are necessary in Java strings, since the \
is an escape character, meaning the following character tells what character is really to be located at this place in the string. By writing \\
you tell the Java compiler to write a single \
character into the string.
The above path is a Windows file system path. On a Unix system (Linux, MacOS, FreeBSD etc.) the above absolute path could look like this:
Path path = Paths.get("/home/jakobjenkov/myfile.txt");
The absolute path is now /home/jakobjenkov/myfile.txt
.
If you used this kind of path on a Windows machine (a path starting with /
) the path would be interpreted as relative to the current drive. For instance, the path
/home/jakobjenkov/myfile.txt
could be interpreted as being located on the C drive. Then the path would correspond to this full path:
C:/home/jakobjenkov/myfile.txt
Creating a Relative Path
创建相对路径的path
A relative path is a path that points from one path (the base path) to a directory or file. The full path (the absolute path) of a relative path is derived by combining the base path with the relative path.
The Java NIO Path
class can also be used to work with relative paths. You create a relative path using the Paths.get(basePath, relativePath)
method. Here are two relative path examples in Java:
注意上面标黄的字体的用法,有两个参数
Path projects = Paths.get("d:\\data", "projects"); Path file = Paths.get("d:\\data", "projects\\a-project\\myfile.txt");
The first example creates a Java Path
instance which points to the path (directory) d:\data\projects
. The second example creates a Path
instance which points to the path (file) d:\data\projects\a-project\myfile.txt
.
When working with relative paths there are two special codes you can use inside the path string. These codes are:
- .
- ..
The .
code means "current directory". For instance, if you create a relative path like this:
Path currentDir = Paths.get(".");
System.out.println(currentDir.toAbsolutePath());
Then the absolute path the Java Path
instance corresponds to will be the directory in which the application executing the above code is executed.
If the .
is used in the middle of a path string it just means the same directory as the path was pointing to at that point. Here is an Path
example illustrating that:
Path currentDir = Paths.get("d:\\data\\projects\.\a-project");
This path will correspond to the path:
d:\data\projects\a-project
The ..
code means "parent directory" or "one directory up". Here is a Path
Java example illustrating that:
Path parentDir = Paths.get("..");
The Path
instance created by this example would correspond to the parent directory of the directory from which the application running this code was started.
If you use the ..
code in the middle of a path string it will correspond to changing one directory up at that point in the path string. For instance:
String path = "d:\\data\\projects\\a-project\\..\\another-project";
Path parentDir2 = Paths.get(path);
The Java Path
instance created by this example will correspond to this absolute path:
d:\data\projects\another-project
The ..
code after the a-project
directory changes directory up the the parent directory projects
and then the path references down into the another-project
directory from there.
The .
and ..
codes also work in combination with the two-string Paths.get()
method. Here are two Java Paths.get()
examples showing simple examples of that:
Path path1 = Paths.get("d:\\data\\projects", ".\\a-project"); Path path2 = Paths.get("d:\\data\\projects\\a-project",
"..\\another-project");
There are more ways that the Java NIO Path
class can be used to work with relative paths. You will learn more about that later in this tutorial.
上面那一大段就是讲当前路径和上一个路径
Path.normalize()
这个normalize()方法就是说打印出你的路径的真实路径,去掉这些../ ./ 换成真实路径
The normalize()
method of the Path
interface can normalize a path. Normalizing means that it removes all the .
and ..
codes in the middle of the path string, and resolves what path the path string refers to. Here is a Java Path.normalize()
example:
String originalPath =
"d:\\data\\projects\\a-project\\..\\another-project"; Path path1 = Paths.get(originalPath);
System.out.println("path1 = " + path1); Path path2 = path1.normalize();
System.out.println("path2 = " + path2);
This Path
example first creates a path string with a ..
code in the middle. Then the example creates a Path
instance from this path string, and prints that Path
instance out (actually it prints Path.toString()
).
The example then calls normalize()
on the created Path
instance, which returns a new Path
instance. This new, normalized Path
instance is then also printed out.
Here is the output printed from the above example:
path1 = d:\data\projects\a-project\..\another-project
path2 = d:\data\projects\another-project
As you can see, the normalized path does not contain the a-project\..
part, as this is redundant. The removed part adds nothing to the final absolute path.
Java NIO Path的更多相关文章
- Java NIO Path接口和Files类配合操作文件
Java NIO Path接口和Files类配合操作文件 @author ixenos Path接口 1.Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路 ...
- Java NIO学习系列七:Path、Files、AsynchronousFileChannel
相对于标准Java IO中通过File来指向文件和目录,Java NIO中提供了更丰富的类来支持对文件和目录的操作,不仅仅支持更多操作,还支持诸如异步读写等特性,本文我们就来学习一些Java NIO提 ...
- Java NIO学习(Path接口、Paths和Files工具类的使用)
NIO学习:Paths和Files工具类的使用 JDK1.7引入了新的IO操作类.在java.nio.file包下,Java NIO Path接口和Files类. Path接口:Path表示的是一个目 ...
- Java NIO 完全学习笔记(转)
本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...
- Java NIO 学习总结 学习手册
原文 并发编程网(翻译):http://ifeve.com/java-nio-all/ 源自 http://tutorials.jenkov.com/java-nio/index.html Java ...
- 海纳百川而来的一篇相当全面的Java NIO教程
目录 零.NIO包 一.Java NIO Channel通道 Channel的实现(Channel Implementations) Channel的基础示例(Basic Channel Exampl ...
- JAVA NIO学习四:Path&Paths&Files 学习
今天我们将学习NIO 的最后一章,前面大部分涉及IO 和 NIO 的知识都已经讲过了,那么本章将要讲解的是关于Path 以及Paths 和 Files 相关的知识点,以对前面知识点的补充,好了言归正传 ...
- Java NIO之拥抱Path和Files
Java面试通关手册(Java学习指南)github地址(欢迎star和pull):https://github.com/Snailclimb/Java_Guide 历史回顾: Java NIO 概览 ...
- JAVA nio 2 定义 Path 类
一旦确认了文件系统上的一个文件或目录,那么就可以定义一个 Path 类来指向它.定义 Path 类可以使用绝对路径.相对路径.路径中带有一个点号“.”(表示当前目录).路径中带有两个点“..”(表示上 ...
随机推荐
- Error: Can't find Python executable "G:\Python27"
错误如题,node-gyp官网介绍不够详细,应设置python.exe的具体绝对路径,如下所示: npm config set python G:\Python27\python.exe
- 'ascii' codec can't decode byte 0xd6 in position 0
使用elastalert,执行python文件时报错: 经查,python命令下输出中文字符时需要将编码指定为gb2312,一开始博主也不知道是输出在控制台的信息编码格式问题,一直以为是博主自己的ya ...
- is和==的区别
is 比较的是内存地址 == 比较的是内容 当两个变量指向同一个对象的时候. is是True, ==也是True
- day2----python的基本类型
本文档的大致内容:(python使用版本3.6.4) 1 数字--int 2 布尔--bool 3 字符串--str 4 元祖--() 5 列表---['a','b'] 6 字典--{} 运算符: ...
- Ruby学习笔记2 : 一个简单的Ruby网站,搭建ruby环境
Ruby on Rails website 的基础是 请求-返回 循环. 首先是浏览器请求服务器, 第二步,Second, in our Rails application, the route ta ...
- python大法好——变量、常量、input()、数据类型、字符串、格式化输出、运算符、流程控制语句、进制、字符编码
python基础知识 1.变量 变量:把程序运算的中间结果临时存到内存里,以备后面的代码可以继续调用. 作用:A.存储数据. B.标记数据. 变量的声明规则: A:变量名只能是字母,数字或下划线任意组 ...
- RestTemplate的异步使用
参考:https://blog.csdn.net/yezhuanxu/article/details/53643248 支持异步调用AsyncRestTemplate @RequestMapping( ...
- C# IIS 服务器上传图片500解决办法
- leetcode986
class Solution: def judgeIntersection(self,a:'Interval',b:'Interval'): #返回值1,bool类型,是否有交集:True-有交集,F ...
- JS 事件 Event
注册事件 target.addEventListener(type, listener, options); 或者 target.addEventListener(type, listener, us ...