当前位置:网站首页>1、File对象学习
1、File对象学习
2022-08-04 20:42:00 【Code攻城狮】
1、创建File对象的三种方式:操作文件夹、文件
/* public File(String pathName): 根据传入的文件夹,文件字符串路径,创建File对象
public File(String parent,String child): 根据传入的[父级别路径][子级路径]创建File对象
public File(String parent,String child): 根据传入的[父级路径][子级路径]创建file对象*/
File f1=new File("D:\learing\a.txt");
if (f1.exists()){
System.out.println("存在");
}
File f2=new File("D:\learing\abc");
if (f1.exists()){
System.out.println("存在");
}
File f3=new File("D:\learing\abc\","a.txt");
if (f1.exists()){
System.out.println("存在");
}
注意事项:
File对象,如果操作的文件|文件夹不存在,是不会抛出异常的
2、相对路径和绝对路径
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qnHqq6M1-1659543372127)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d4bb1ab7c7ba4639b041ec159203f64d~tplv-k3u1fbpfcp-watermark.image?)]](/img/17/75c3273283be10af7eeb4a3e1656fe.png)
a1.txt在盘符下所存放的位置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YnHy20st-1659543372128)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/719b2cba9e564edab07fd122b60b1cbf~tplv-k3u1fbpfcp-watermark.image?)]](/img/e7/be59237c311eb7939902afd0bcf6bc.png)
注意有的人创建的时候,可能会报出一个错误问题,这个问题是因为IDEA 环境设置问题,把其相对路径设置在了demo下了,所以创建的时候,路径位置就不能是demo\
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eDCgE78g-1659543372129)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0b2ddef52785481d875c589a5e11bdfc~tplv-k3u1fbpfcp-watermark.image?)]](/img/ca/d1b128d2266629c5ac1e78e4274e7b.png)
系统不能识别demo这个路径,这是因为我们IDEA路径设置问题
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FKfx7nlk-1659543372129)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f19a08e5e62942b19b4af323c6b53873~tplv-k3u1fbpfcp-watermark.image?)]](/img/42/151f415744cd594cefab296c812c77.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rNDFepae-1659543372129)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f0c26efce3564f96b9509e8427066fd3~tplv-k3u1fbpfcp-watermark.image?)]](/img/05/ca8cf2945b39e9b32ddaba771cf383.png)
修改后:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8zD9Hu6z-1659543372130)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7cf3add441ba42808192218b18f17a7e~tplv-k3u1fbpfcp-watermark.image?)]](/img/2e/2a0203f4e175c4739c516bca9f2ef8.png)
3、File对象的一些方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mz8556F1-1659543372130)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/765128a38f9e4b4eb7267afc5479aec5~tplv-k3u1fbpfcp-watermark.image?)]](/img/c6/a82aab327b7a2d7962419e61b0faac.png)
File f=new File("demo\a.txt");
//1、创建文件对象
boolean newFile = f.createNewFile();
System.out.println(newFile); //如果文件缺失已经存在,则为false,否则返回true
//2、判断是否为文件夹
System.out.println(f.isDirectory()); //false
//3、判断是否为文件
System.out.println(f.isFile()); //true
//4、返回文件的字节个数
System.out.println(f.length());
//5、返回文件的绝对路径
System.out.println(f.getAbsoluteFile());
//6、获取定义文件时候的路径
System.out.println(f.getPath());
//7、获取文件的名字,并且带后缀
System.out.println(f.getName());
//8、返回文件最后一次修改的时间
long l = f.lastModified();
System.out.println(l);
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OMGZhJq4-1659543372131)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8cd3bd7fb02144d5a82398f7065f6c55~tplv-k3u1fbpfcp-watermark.image?)]](/img/f3/509c2eec80e361bc63f55f4a2f6d1b.png)
4、创建文件夹
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OfIqQF3X-1659543372131)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/982963e8b1c4434c85ff175a7cb89230~tplv-k3u1fbpfcp-watermark.image?)]](/img/62/46be053960675ff8c0c295ee5e2a68.png)
删除问价文件是delete
f1.delete
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C7sisgCx-1659543372131)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/61c9f782f4dc4b50a9bf97db931210e7~tplv-k3u1fbpfcp-watermark.image?)]](/img/5c/49a77f6326135429e26eda62537168.png)
边栏推荐
猜你喜欢
随机推荐
帝国CMS仿核弹头H5小游戏模板/92game帝国CMS内核仿游戏网整站源码
刷题-洛谷-P1319 压缩技术
web 应用开发最佳实践之一:避免大型、复杂的布局和布局抖动
How to make good use of builder mode
EasyUi常用代码
【学术相关】清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
多用户同时远程登录连接到一台服务器
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
C语言基础[通俗易懂]
宝塔实测-搭建中小型民宿酒店管理源码
Getting Started with Lattice Passwords
MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
run command for node
动态数组底层是如何实现的
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
Tear down the underlying mechanism of the five JOINs of SparkSQL
c语言小项目(三子棋游戏实现)
idea源码无法下载
AWS SES 的监控和告警
【TypeScript】深入学习TypeScript枚举









