当前位置:网站首页>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
}边栏推荐
- 杭电多校-Slipper-(树图转化+虚点建图)
- Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
- 哎,又跟HR在小群吵了一架!
- 深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
- MySQL Query Exercise (1)
- Use serve to build a local server
- 【id类型和NSObject指针 ObjectIve-C中】
- 8.Haproxy 搭建Web集群
- Implementing a server-side message active push solution based on SSE
- MRS: Alluxio的使用介绍
猜你喜欢

企业直播风起:目睹聚焦产品,微赞拥抱生态

7. The principle description of LVS load balancing cluster

Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model

4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver

The Shell function

如何简化现代电子采购的自动化?

汇编语言之栈

案例 | 重庆银行流动数据安全挑战及应对实践

TL431的基本特性以及振荡电路

深度学习之 10 卷积神经网络3
随机推荐
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
系统设计.秒杀系统
if,case,for,while
Explain详解与实践
基于Qt的目录统计QDirStat
嵌入式数据库开发编程MySQL(全)
类如何只能静态分配和只能动态分配
【id类型和NSObject指针 ObjectIve-C中】
unity框架之缓存池
Learn iframes and use them to solve cross-domain problems
自定义通用分页标签01
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Jenkins 导出、导入 Job Pipeline
机器学习之视频学习【更新】
LeetCode每日一题(2285. Maximum Total Importance of Roads)
7-1 LVS+NAT 负载均衡群集,NAT模式部署
MRS: Introduction to the use of Alluxio
7-3 LVS+Keepalived集群叙述与部署
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
Functions, recursion and simple dom operations