当前位置:网站首页>Simple operation of the file system
Simple operation of the file system
2022-08-04 04:06:00 【Master_hl】
1. 文件的存储位置
文件是存储在硬盘上的!!
Simple understanding of the difference between hard disk and memory:
1.内存存储空间小,硬盘空间大.
2.内存访问速度快,硬盘访问速度慢.
3.内存成本高,硬盘便宜.
4.Memory breakpoint data is lost,The hard disk breakpoint data is still there.
Files are also managed by the operating system,There is a dedicated module in the operating system kernel,文件系统.
2. File 概述
属性
修饰符及类型 | 属性 | 说明 |
static String | pathSeparator | 依赖于系统的路径分隔符 '/',String 类型的表示 |
static char | pathSeparator | 依赖于系统的路径分隔符 '/',char 类型的表示 |
构造方法
方法 | 说明 |
File(File parent, String child) | 根据父目录 + 孩子文件路径,创建一个新的 File 实例 |
File(String pathname) | 根据文件路径创建一个新的 File 实例,路径可以是绝对路径或者相对路径 |
File(String parent, String child) | 根据父目录 + 孩子文件路径,创建一个新的 File 实例,父目录用路径表示 |
方法
修饰符及返回值类型 | 方法 | 说明 |
String | getParent() | 返回 File 对象的父目录文件路径 |
String | getName() | 返回 FIle 对象的纯文件名称 |
String | getPath() | 返回 File 对象的文件路径 |
String | getAbsolutePath() | 返回 File 对象的绝对路径 |
String | getCanonicalPath() | 返回 File 对象的修饰过的绝对路径 |
boolean | exists() | 判断 File 对象描述的文件是否真实存在 |
boolean | isDirectory() | 判断 File 对象代表的文件是否是一个目录 |
boolean | isFile() | 判断 File 对象代表的文件是否是一个普通文件 |
boolean | createNewFile() | 根据 File 对象,自动创建一个空文件.成功创建后返回 true |
boolean | delete() | 根据 File 对象,删除该文件.成功删除后返回 true |
void | deleteOnExit() | 根据 File 对象,标注文件将被删除,删除动作会到 JVM 运行结束时才会进行 |
String[] | list() | 返回 File 对象代表的目录下的所有文件名 |
File[] | listFiles() | 返回 File 对象代表的目录下的所有文件,以 File 对象表示 |
boolean | mkdir() | 创建 File 对象代表的目录 |
boolean | mkdirs() | 创建 File 对象代表的目录,如果必要,会创建中间目录 |
boolean | renameTo(File dest) | 进行文件改名,也可以视为我们平时的剪切、粘贴操作 |
boolean | canRead() | 判断用户是否对文件有可读权限 |
boolean | canWrite() | 判断用户是否对文件有可写权限 |
绝对路径:以盘符开头的路径,称为 "绝对路径".
相对路径:以 . 或者 .. The path at the beginning is called "相对路径".
代码示例1
getA series of method demonstrations
public static void main(String[] args) throws IOException {
File file = new File("./test.txt");
System.out.println(file.getParent());
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.getAbsoluteFile());
System.out.println(file.getCanonicalFile());
}
代码示例2
普通文件的创建
public static void main(String[] args) throws IOException {
// 前面没写 ./ ,也相当于是 ./ , ./can be ignored.
File file = new File("hello.txt");
System.out.println(file.exists()); // false
System.out.println(file.isDirectory()); // false
System.out.println(file.isFile()); // false
System.out.println("==============");
file.createNewFile();
System.out.println(file.exists()); // true
System.out.println(file.isDirectory()); // false
System.out.println(file.isFile()); // true
}
代码示例3
普通文件的删除
public static void main(String[] args) throws InterruptedException {
File file = new File("hello.txt");
//file.delete();
//System.out.println(file.exists()); // false
// It is only deleted when the program exits
file.deleteOnExit(); // 用来创建临时文件
Thread.sleep(5000);
System.out.println(file.exists()); // true
}
代码示例4
创建目录
public static void main(String[] args) {
File file = new File("test/aa/bb");
System.out.println(file.exists()); // false
System.out.println(file.isDirectory()); // false
System.out.println("=============");
//file.mkdir(); // 创建单级目录,Use this to create multilevel directories,The following will output two false
file.mkdirs(); // 创建多级目录
System.out.println(file.exists());
System.out.println(file.isDirectory());
}
代码示例5
文件重命名
public static void main(String[] args) {
File file1 = new File("test1.txt");
File file2 = new File("test2.txt");
file1.renameTo(file2); // 把文件 file1 的名字改成 test2.txt
}
边栏推荐
- 数据集类型转换—TFRecords文件
- Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
- drools从下载到postman请求成功
- 元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
- Based on the statistical QDirStat Qt directory
- 目标检测-中篇
- 用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
- 7.LVS负载均衡群集之原理叙述
- Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
- Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
猜你喜欢
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
深度学习之 10 卷积神经网络3
2022年软件测试——精选金融银行面试真题
网络工程师入门必懂华为认证体系,附系统学习路线分享
如何简化现代电子采购的自动化?
【项目实现】Boost搜索引擎
打造一份优雅的简历
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
Basic form validation process
随机推荐
docker+bridge+redis master-slave+sentry mode
怎么把elastic中的异常登录ip和日志自动导出或抓取到数据库中?
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
Embedded database development programming MySQL (full)
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Introduction to mq application scenarios
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
SQL query String field less than 10 how to check
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
十一种概率分布
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
【技巧】借助Sentinel实现请求的优先处理
7-3 LVS+Keepalived集群叙述与部署
Based on the statistical QDirStat Qt directory
Polygon zkEVM网络节点
2022杭电多校联赛第五场 题解
复制带随机指针的链表
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
内网服务器访问远程服务器的端口映射
图像处理之Bolb分析(一)