当前位置:网站首页>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、相对路径和绝对路径
a1.txt在盘符下所存放的位置
注意有的人创建的时候,可能会报出一个错误问题,这个问题是因为IDEA 环境设置问题,把其相对路径设置在了demo下了,所以创建的时候,路径位置就不能是demo\
系统不能识别demo这个路径,这是因为我们IDEA路径设置问题
修改后:
3、File对象的一些方法
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);
4、创建文件夹
删除问价文件是delete
f1.delete
边栏推荐
- 数据安全解决方案的发展
- 刷题-洛谷-P1179 数字统计
- vscode离线安装插件方法
- 简单理解 JS 事件循环
- 知识分享|如何设计有效的帮助中心,不妨来看看以下几点
- 面试官:索引为什么会失效?
- Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership
- After encountering MapStruct, the conversion between PO, DTO and VO objects is no longer handwritten
- composition-api
- KubeSphere简介,功能介绍,优势,架构说明及应用场景
猜你喜欢
随机推荐
基于单向链表结构的软件虚拟定时器的设计与构建
How to make good use of builder mode
STP --- 生成树协议
嵌入式分享合集28
【随记】新一天搬砖 --20220727
Latex分章节、分段落编译:input{}与include{}的区别
新式茶饮,卷完水果还能卷什么?
SAP ABAP OData 服务如何支持 $select 有选择性地仅读取部分模型字段值试读版
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
深度解析:为什么跨链桥又双叒出事了?
Tear down the underlying mechanism of the five JOINs of SparkSQL
ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
win10 uwp 使用 ScaleTransform 放大某个元素
【TypeScript】深入学习TypeScript枚举
数据安全解决方案的发展
简述@RequestParam与@RequestBody参数注解
Interviewer: How is the expired key in Redis deleted?
【SQL】触发器同步表数据
关于 SAP 电商云 Spartacus UI SSR 的 state transfer 问题
C语言——青蛙跳台阶(递归)