Parh,Paths与Files是在JDK7中新添加进来的,都在 java.nio.file包下面,使用时注意JDK版本
一、Parh,Paths
1、java.nio.file.Paths类
作用:通过一个路径字符串来构造 Path对象。
Paths类是 final修饰的,仅由两个static方法组成。
注意:文件系统的分隔符(Unix文件系统是 / ,Windows是 \,/也可以 )
路径中的第一个部件可以是根部件,例如 / 或 C:\ 。以根部件开始的是绝对路径;否则就是相对路径。
@Test
public void testPaths() throws URISyntaxException {
Path path = Paths.get("E:/java");
System.out.println(path); // E:\java
Path path2 = Paths.get("E:/java","/赵云.jpg");
System.out.println(path2);// E:\java\赵云.jpg
URI uri = new URI("file:///E:/java/赵云.jpg");
Path path3 = Paths.get(uri);
System.out.println(path3); //E:\java\赵云.jpg
}
2、java.nio.file.Path接口
可用于在文件系统中定位文件的对象。 它通常表示系统相关的文件路径。
Path用来表示文件路径和文件。可以有多种方法来构造一个Path对象来表示一个文件路径,或者一个文件。Path接口可以取代File类的。Path类包含一些简单方法操作,具体查看API.
1)判断路径以某一分层路径的开始或结束
-
-
boolean
endsWith(Path other)
测试此路径是否以给定的路径结束。
boolean
endsWith(String other)
测试此路径是否以
Path
结束,通过转换给定的路径字符串,完全按照endsWith(Path)
方法指定的方式构建。boolean
startsWith(Path other)
测试此路径是否以给定的路径开始。
boolean
startsWith(String other)
测试此路径是否以
Path
,通过转换给定的路径字符串,按照startsWith(Path)
方法指定的方式构建。
-
@Test
public void testPaths() throws URISyntaxException {
Path path = Paths.get("E:/java","/赵云.jpg");
System.out.println(path);// E:\java\赵云.jpg
System.out.println(path.endsWith("jpg"));//false 以某一分层路径
System.out.println(path.endsWith("赵云.jpg"));//true
System.out.println(path.startsWith(Paths.get("E:/java")));// true
}
2)File和Path之间的转换,File和URI之间的转换
@Test
public void testPaths() throws URISyntaxException {
Path path = Paths.get("E:/java","/赵云.jpg");
File file = path.toFile();
URI uri = path.toUri();
file.toPath();
file.toURI();
}
-
-
Path
getParent()
返回 父路径 ,或
null
如果此路径没有父。Path
getRoot()
返回此路径的根组分作为
Path
对象,或null
如果该路径不具有根组件。Path
getFileName()
将此路径表示的文件或目录的名称返回为
Path
对象。FileSystem
getFileSystem()
返回创建此对象的文件系统。
Path
getName(int index)
返回此路径的名称元素作为
Path
对象。boolean
isAbsolute()
告诉这条路是否是绝对的。
Iterator<Path>
iterator()
返回此路径的名称元素的迭代器。
Path
toRealPath(LinkOption... options)
返回现有文件的 真实路径。
-
二、java.nio.file.Files类
Files类是非常好用的 io操作工具类,该类包含了对文件,目录或其他类型文件进行操作的静态方法。例如文件复制,移动,删除,读取文件内容,写入文件内容等,下面介绍一些常用的方法:
1、文件复制
-
-
static long
copy(InputStream in, Path target, CopyOption... options)
将输入流中的所有字节复制到文件。
static long
copy(Path source, OutputStream out)
将文件中的所有字节复制到输出流。
static Path
copy(Path source, Path target, CopyOption... options)
将文件复制到目标文件。
-
@Test
public void testPaths() throws Exception {
Path path = Paths.get("E:/java","/赵云image.jpg");
FileInputStream inputStream = new FileInputStream(new File("E:/java/赵云.jpg"));
Files.copy(inputStream, path);
}
2、目录,文件,删除
-
-
static Path
createDirectories(Path dir, FileAttribute<?>... attrs)
首先创建所有不存在的父目录来创建目录。
static Path
createDirectory(Path dir, FileAttribute<?>... attrs)
创建一个新的目录。
static Path
createFile(Path path, FileAttribute<?>... attrs)
创建一个新的和空的文件,如果该文件已存在失败。
static void
delete(Path path)
删除文件。
static boolean
deleteIfExists(Path path)
删除文件(如果存在)。
static boolean
exists(Path path, LinkOption... options)
测试目录/文件是否存在。
static boolean
isDirectory(Path path, LinkOption... options)
测试文件是否是目录。
static boolean
isExecutable(Path path)
测试文件是否可执行。
static boolean
isReadable(Path path)
测试文件是否可读。
static long
size(Path path)
返回文件的大小(以字节为单位)。
-
@Test
public void testPaths() throws Exception {
Path path = Paths.get("E:/java000");
if(!Files.exists(path)){
Files.createDirectories(path);
}
System.out.println(Files.isDirectory(path)); // true
Path path2 = Paths.get("E:/java000/text000.txt");
if(!Files.exists(path2)){
Files.createFile(path2);
}
Files.deleteIfExists(path2);
}
3、文件的移动
-
-
static Path
move(Path source, Path target, CopyOption... options)
将文件移动或重命名为目标文件。
-
public void testPaths() throws Exception {
Path path = Paths.get("E:/java","/赵云image.jpg");
Path destpath = Paths.get("E:/赵云move.jpg");
// 移动:原文件不存在,移动并重命名为赵云move.jpg
Files.move(path,destpath);
}
4、遍历一个文件夹目录获取文件
-
-
static DirectoryStream<Path>
newDirectoryStream(Path dir)
打开一个目录,返回一个
DirectoryStream
以遍历目录中的所有条目。static DirectoryStream<Path>
newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
打开一个目录,返回一个
DirectoryStream
来迭代目录中的条目。static DirectoryStream<Path>
newDirectoryStream(Path dir, String glob)
打开一个目录,返回一个
DirectoryStream
来迭代目录中的条目。
-
@Test
public void testPaths() throws Exception {
Path path = Paths.get("E:/java/file03");
if(Files.exists(path)){
DirectoryStream<Path> paths = Files.newDirectoryStream(path);
paths.forEach(p -> {
if(!Files.isDirectory(p)){
System.out.println(p.getFileName());
}
});
}
}
4- 5-
5、遍历一个文件夹目录获取文件,(包含嵌套文件夹中文件)
-
-
static Path
walkFileTree(Path start, FileVisitor<? super Path> visitor)
走一个文件树。
static Path
walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor)
走一个文件树。
-
walkFileTree方法需要FileVisitor类型的参数,FileVisitor是一个接口,public class SimpleFileVisitor<T> implements FileVisitor<T>
遍历文件和目录时会触发定义在FileVisitor中的方法:
-
-
FileVisitResult
postVisitDirectory(T dir, IOException exc)
在目录中的条目之后调用目录,并且已经访问了所有的后代。
FileVisitResult
preVisitDirectory(T dir, BasicFileAttributes attrs)
在目录中的条目被访问之前调用一个目录。
FileVisitResult
visitFile(T file, BasicFileAttributes attrs)
为目录中的文件调用。
FileVisitResult
visitFileFailed(T file, IOException exc)
为无法访问的文件调用。
-
上面4个方法都返回 FileVisitResult 对象,定义了4种访问后的行为。
-
-
CONTINUE
继续。SKIP_SIBLINGS
继续,不要访问此文件或目录的 兄弟姐妹 。SKIP_SUBTREE
继续访问此目录中的条目。TERMINATE
终止。
-
@Test
public void testPaths() throws IOException {
Path path = Paths.get("E:/java/file03");
if(Files.exists(path)){
Files.walkFileTree(path
, new SimpleFileVisitor<Path>() {
// 访问文件时触发
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.endsWith("abc雪02.jpg")) {
System.out.println("已经找到abc雪02.jpg文件!停止查找!");
return FileVisitResult.TERMINATE;
}
System.out.println("正在访问文件" + file);
return FileVisitResult.CONTINUE;
}
// 访问目录时触发
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("正在访问目录:" + dir);
return FileVisitResult.CONTINUE;
}
});
}
}
以官方API为准,自己敲一遍熟悉
ends~