当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
暴雨中的人
Win10 uwp use ScaleTransform magnify an element
如何用好建造者模式
Chrome安装zotero connector 插件
vscode离线安装插件方法
win10终端中如何切换磁盘
在vs code中进行本地调试和开启本地服务器
Latex分章节、分段落编译:input{}与include{}的区别
C语言小笔记+题
2022年国内手机满意度榜单:华为稳坐国产品牌第一
win10 uwp 修改图片质量压缩图片
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
After the tester with 10 years of service "naked resignation" from the big factory...
Tear down the underlying mechanism of the five JOINs of SparkSQL
WIN10系统如何开启终端
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
KubeSphere简介,功能介绍,优势,架构说明及应用场景
知识分享|如何设计有效的帮助中心,不妨来看看以下几点
Debug locally and start the local server in vs code
搭建MyCat2一主一从的MySQL读写分离









