当前位置:网站首页>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
}边栏推荐
- Implementing a server-side message active push solution based on SSE
- 张量篇-应用案例
- 千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
- 解决问题遇到的问题
- Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
- SVM介绍以及实战
- 【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
- 使用serve搭建本地服务器
- Significant differences between Oracle and Postgresql in PLSQL transaction rollback
- 基于 SSE 实现服务端消息主动推送解决方案
猜你喜欢
随机推荐
Take care of JVM performance optimization (own note version)
Mockito单元测试
马尔可夫链
MySQL query optimization and tuning
Mockito unit testing
Stop behind.
Hey, I had another fight with HR in the small group!
一个属于程序员的七夕节!
目标检测-中篇
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
张量篇-应用案例
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
网络工程师入门必懂华为认证体系,附系统学习路线分享
Tensors - Application Cases
【源码】使用深度学习训练一个游戏
FPGA parsing B code----serial 3
Based on the statistical QDirStat Qt directory
图像处理之Bolb分析(一)
mq应用场景介绍
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing









