网站首页 > 技术教程 正文
JAVA中的文件操作1-如何获取文件信息,创建文件
JAVA7和JAVA8对JAVA的文件操作进行了史诗级增强,使得文件操作变得非常简单,下面简单介绍一下。
Path类表示文件路径
要访问或者创造一个文件,首先我们需要得到文件的路径,java.nio.file.Path就是可以表示路径的类,它既可以表示目录,也可以表示文件。
我们可以用Paths来生成Path,在生成时,可以有以下几种方式:
- 直接指定文件路径
- 指定父目录,子文件名的方式
- 通过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还可以进一步获取文件权限,文件是否隐藏,文件是否可读/可写/可执行/文件大小等更多信息,就不一一演示了。
创建文件
创建文件分成两种情况:
- 目录存在,直接新建一个文件
- 目录不存在,甚至目录的父目录也不存在,需要级联创建目录后再创建文件
下面依次演示:
直接新建一个文件,使用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);
}
- 上一篇: 用VBs创建文件(vbs文件编写)
- 下一篇: VBA操作文件四大方法「3」(vba帮助文档 chm)
猜你喜欢
- 2024-10-25 Java 17 NIO 知识点 Files 操作(java17课)
- 2024-10-25 PE格式:新建节并插入代码(怎么用pe创建新用户)
- 2024-10-25 入门Java不迷路!一篇教你搞懂Java 「File类」的概述和方法
- 2024-10-25 Qt 之QTemporaryFile用法(创建临时文件)
- 2024-10-25 EndNote使用技巧(4)之七种方法导入文献?一文全搞定
- 2024-10-25 Vue+Element UI实现断点续传、分片上传、秒传
- 2024-10-25 共享数据资源,VBA代码导入已有文本数据文件的方法
- 2024-10-25 (12)文本文件操作参考(文本的基本操作)
- 2024-10-25 使用StreamWriter类的对文件创建写入
- 2024-10-25 大文件上传:秒传、断点续传、分片上传
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)