当前位置:网站首页>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
}
边栏推荐
- 初识Numpy
- 高效IO模型
- SQL injection in #, - +, - % 20, % 23 is what mean?
- 图像处理之Bolb分析(一)
- MySQL query optimization and tuning
- 2022年软件测试——精选金融银行面试真题
- FPGA解析B码----连载3
- 6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
- 使用serve搭建本地服务器
- Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
猜你喜欢
Polygon zkEVM网络节点
LeetCode每日一题(2285. Maximum Total Importance of Roads)
函数,递归以及dom简单操作
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
base address: environment variable
7-1 LVS+NAT 负载均衡群集,NAT模式部署
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
马尔可夫链
深度学习之 10 卷积神经网络3
随机推荐
怎么把elastic中的异常登录ip和日志自动导出或抓取到数据库中?
SQL interview Questions
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
机器学习模型的“可解释性”
帮助企业实现数字化转型成功的八项指导原则
6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
mysql索引笔记
The video of machine learning to learn [update]
高效IO模型
if,case,for,while
Basic form validation process
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
MRS: Alluxio的使用介绍
文件系统的简单操作
Take care of JVM performance optimization (own note version)
Reproduce 20-character short domain name bypass
8.Haproxy 搭建Web集群
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》