java文件操作---Path和Files

Path

Path是一个接口,表示一个目录名序列,还可以表示一个文件。

创建Path的方法如下:

1
2
3
4
//Paths类的get方法
public static Path get(String first, String... more)

public static Path get(URI uri)

例如:

1
2
3
4
5
Path path = Paths.get("D:/", "abc");
Path path2 = Paths.get("/abc");

URI u = URI.create("file:///D:/abc/a");
Path path3 = Paths.get(u);

Path的常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Path resolve(Path other)
Path resolve(String other)

如果`other`是绝对路径,那么返回`other`;否则,返回通过连接`this`和`other`获得路径。

Path resolveSibling(Path other)
Path resolveSibling(String other)

根据指定路径的父路径产生兄弟路径

Path relativize(Path other)
利用`this`进行解析,相对于`other`的相对路径

Path normalize()
移除诸如`.`和`...`等冗余的路径元素

Path toAbsolutePath()
返回于该路径等价的绝对路径

Path getParent()
返回父路径,或者在该路径没有父路径时,返回`null`

Path getFileName()
返回该路径的最后一个目录或文件名,或者在该路径没有任何目录时,返回`null`

Path getRoot()
返回该路径的根目录,或者在该路径没有任何根目录时返回`null`

File toFile()
从该路径创建一个`File`对象

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Main {

public static void main(String[] args) throws Exception {
Path path = Paths.get("/abc/a/c/m.txt");
System.out.println(path.toString());
System.out.println(path.toAbsolutePath().toString());//返回绝对路径
System.out.println(path.toUri().toString());//返回uri
System.out.println(path.getNameCount());//返回路径数量
System.out.println(path.getParent());//获取父路径
System.out.println(path.getRoot());//获取路径的根目录
System.out.println(path.getFileName());//返回路径的最后一个目录
System.out.println(path.resolveSibling("d/e").toAbsolutePath());
System.out.println(path.resolveSibling("f").toAbsolutePath());
}
}

//结果如下:
\abc\a\c\m.txt
D:\abc\a\c\m.txt
file:///D:/abc/a/c/m.txt
4
\abc\a\c
\
m.txt
D:\abc\a\c\d\e
D:\abc\a\c\f

Files

Files可以让我们很简单地处理常用的文件操作。

读写文件

1
2
3
4
5
6
7
8
9
10
11
12
13
static byte[] readAllBytes(Path path)
static List<String> readAllLines(Path,Charset charset)
读取文件

static Path write(Path path,byte[] content,OpenOption...op)
static Path write(Path path,Iterable<? extends CharSequence> content,OpenOption op)
将指定内容写入文件中,并返回path

static InputStream newInputStream(Path path,OpenOption... op)
static InputStream newOutputStream(Path path,OpenOption... op)
static BufferedReader newBufferedReader(Path path,Charset charset)
static BufferedWriter newBufferedWriter(Path path,Charset charset,OpenOption... op)
打开一个文件,用于读入或读写(注意,这几个方法适合大文件的读入或读写,中小文件应该用上面的方法)

关于OpenOption,下面会介绍。

创建文件和目录

1
2
3
4
5
6
7
8
9
10
11
static Path createFile(Path path, FileAttribute<?>... attrs)
static Path createDirectory(Path dir, FileAttribute<?>... attrs)
static Path createDirectories(Path dir, FileAttribute<?>... attrs)
创建一个文件或目录,`createDirectory`要求路径中除最后一个部件外,其他部分都存在;
`createDirectories`不要求;`createFile`创建空文件;`FileAttribute`表示文件或目录的属性

static Path createTempDirectory(Path dir,String prefix,FileAttribute<?>... attrs)
static Path createTempDirectory(Path parentDir,String prefix,String suffix,FileAttribute<?>... attrs)
static Path createTempFile(Path dir,String prefix,FileAttribute<?>... attrs)
static Path createTempFile(Path parentDir,String prefix,String suffix,FileAttribute<?>... attrs)
创建临时文件,并返回创建的文件和目录的路径

复制,移动和删除文件

1
2
3
4
5
6
7
8
9
10
11
12
static Path Copy(Path from,Path to,CopyOption...op)
static Path move(Path from,Path to,CopyOption...op)
将`from`复制或移动到`to`位置,并返回`to`

static long Copy(InputStream from,Path to,CopyOption...op)
static long Copy(InputStream from,OutputStream to,CopyOption...op)
从输入流复制到文件中,或者从文件复制到输出流中,返回复制的字节数

static void delete(Path path)
static boolean deleteIfExists(Path path)
删除指定的文件或路径。第一种方法在文件或路径不存在时抛出异常,而第二种
方法在这种情况下返回`false`

用于操作文件的标准选项


获取文件信息

1
2
3
4
5
6
7
8
9
10
static boolean exists(Path path)
static boolean isHidden(Path path)
static boolean isReadable(Path path)
static boolean isWritable(Path path)
static boolean isExecutable(Path path)
static boolean isRegularFile(Path path)
static boolean isDirectory(Path path)
static boolean isSymbolicLink(Path path)
static long size(Path path)
static A readAttributes(Path path,Class<A> type,LinkOption...op)//读取类型为A的属性

可以通过readAttributes方法获取BasicFileAttributes接口,这个接口储存了文件的基本信息,例如:

1
BasicFileAttributes attributes = Files.readAttributes(Paths.get("/abc/a"),BasicFileAttributes.class);

BasicFileAttributes的方法有

1
2
3
4
5
6
7
8
FileTime creationTime()
FileTime lastAccessTime()
FileTime lastModifiedTime()
boolean isRegularFile()
boolean isDirectory()
boolean isSymbolicLink()
boolean size()
Object fileKey()

访问目录的项