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 类可以使用绝对路径.相对路径.路径中带有一个点号“.”(表示当前目录).路径中带有两个点“..”(表示上 ...
随机推荐
- git解决冲突插件之Beyond Compare
Beyond Compare主要作用: 1. 可以比较文件.文件夹的差异: 2. 将一个文件或文件夹的两个不同版本进行变更合并,生成一个输出. 基于以上两个特性,可以将beyond compare集成 ...
- show processlist结果筛选
在MySQL里面 show variables where variable_name like '%auto%' 这条语句可以正常执行,但是 show processlist where host ...
- isinstance, type, issubclass
isinstance: 判断你给对象是否是xx类型的. (向上判断)type: 返回xxx对象的数据类型issubclass: 判断xxx类是否xxx的子类 class Animal: def eat ...
- [Unity工具]批量修改字体
效果图: using System.IO; using System.Text; using UnityEditor; using UnityEngine; using UnityEngine.UI; ...
- 输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。程序设计思路。
将文件内容读取后存入StringBuffer中. 利用函数将段落分割成字符串,按(“,”,“.”,“!”,“空格”,“回车”)分割,然后存入数组中. 遍历数组,并统计每个单词及其出现的次数. 要求出文 ...
- python中赋值-浅拷贝-深拷贝之间的关系
赋值: 变量的引用,没有拷贝空间 对象之间赋值本质上 是对象之间的引用传递而已.也就是多个对象指向同一个数据空间. 拷贝的对象分两种类型: . 拷贝可变类型 浅拷贝: 只拷贝第一层数据,不关心里面的第 ...
- 重识linux-循环执行的例行性工作调度
重识linux-循环执行的例行性工作调度 1 用户的设置 1)/etc/cron.allow 可以使用的账号,在这个文件内 2)/etc/cron.deny 不可以的放在这个文件里面 allow的优 ...
- 代码: js: 数值操作
数值转换: 将 32000 这样的数字,转换为“3.2万” //将32000 这样的数字,转换为 “3.2万” var price = parseInt('31999'); var price2 = ...
- 04.给linux用户添加sudo权限
linux给用户添加sudo权限: 有时候,linux下面运行sudo命令,会提示类似: xxxis not in the sudoers file. This incident will be r ...
- VC中链接错误,提示string重定义
VC链接错误,说是string已经有了实现了,只要 rebuild 一下好了. Linking...LINK : warning LNK4075: ignoring '/EDITANDCONTINUE ...