JAVA中的文件操作1-如何获取文件信息,创建文件

JAVA中的文件操作1-如何获取文件信息,创建文件要访问或者创造一个文件,首先我们需要得到文件的路径,java.nio.file.Path就是可以表示路径的类,它既可以表示目录,也可以表示文件。

大家好,欢迎来到IT知识分享网。JAVA中的文件操作1-如何获取文件信息,创建文件"

JAVA中的文件操作1-如何获取文件信息,创建文件

JAVA7和JAVA8对JAVA的文件操作进行了史诗级增强,使得文件操作变得非常简单,下面简单介绍一下。

Path类表示文件路径

要访问或者创造一个文件,首先我们需要得到文件的路径,java.nio.file.Path就是可以表示路径的类,它既可以表示目录,也可以表示文件。
我们可以用Paths来生成Path,在生成时,可以有以下几种方式:

  1. 直接指定文件路径
  2. 指定父目录,子文件名的方式
  3. 通过URI来标识

下面依次演示一下:

// 直接写路径
Path directPath = Paths.get("D:\\opensource\\springboot-test");
System.out.println(directPath.toAbsolutePath());

// 写父目录+子路径
Path subPath = Paths.get("D:\\opensource", "springboot-test");
System.out.println(subPath.toAbsolutePath());

// 写URI
Path uriPath = Paths.get(URI.create("file:///D:/opensource/springboot-test"));
System.out.println(uriPath.toAbsolutePath());

使用Files工具类对Path进行操作

在拿到Path对象后,我们可能还需要进一步处理,比如增删文件,读取/写入文件,获取路径是否为目录/文件等,这些可以通过Files来完成。
Files类指的是java.nio.file.Files,提供了大量和文件相关的操作方法,有兴趣的可以自行查阅官方文档。

获取Path的基本信息

Path自己提供了获取父目录,获取根目录的方法,如下所示:

Path directPath = Paths.get("D:\\opensource\\springboot-test");
System.out.println("path :");
System.out.println(directPath.toAbsolutePath());
System.out.println("getParent: " + directPath.getParent());
System.out.println("getRoot: " + directPath.getRoot());

输出如下:

path :
D:\opensource\springboot-test
getParent: D:\opensource
getRoot: D:\

除了目录树信息外,我们还可以通过Files提供的工具类获取Path的其他信息,比如是否为目录,是否为文件等,如下所示:

Path directPath = Paths.get("D:\\opensource\\springboot-test");
System.out.println("path :");
System.out.println(directPath.toAbsolutePath());
System.out.println("isDirectory: " + Files.isDirectory(directPath));
System.out.println("isRegularFile: " + Files.isRegularFile(directPath));
System.out.println("exists: " + Files.exists(directPath));

输出如下:

D:\opensource\springboot-test
isDirectory: true
isRegularFile: false
exists: true

除了上面这些信息,Files还可以进一步获取文件权限,文件是否隐藏,文件是否可读/可写/可执行/文件大小等更多信息,就不一一演示了。

创建文件

创建文件分成两种情况:

  1. 目录存在,直接新建一个文件
  2. 目录不存在,甚至目录的父目录也不存在,需要级联创建目录后再创建文件

下面依次演示:

直接新建一个文件,使用Files.createFile

String basePath = "D:\\opensource\\springboot-test";
Path notExistFile = Paths.get(basePath, "notExist.txt");
if (!Files.exists(notExistFile)){
    Files.createFile(notExistFile);
}

目录不存在,新建目录后再创建文件,使用Files.createDirectories创建。
如果路径已存在,而且是目录,调用Files.createDirectories不会有影响。但是如果是一个文件,就会抛出异常了。
一次性可以创建多层不存在的目录。

String basePath = "D:\\opensource\\springboot-test";
String notExistDir = "notExist";
// D:\opensource\springboot-test\notExist\notExist\notExist.txt
// 目录不存在,父目录也不存在。
Path notExistPathAndFile = Paths.get(basePath, notExistDir, notExistDir, "notExist.txt");
Files.createDirectories(notExistPathAndFile.getParent());
if (!Files.exists(notExistPathAndFile)) {
    Files.createFile(notExistPathAndFile);
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/39144.html

(0)
上一篇 2024-04-16 15:00
下一篇 2024-04-16 21:15

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信