当前位置:网站首页>6.File类
6.File类
2022-08-09 09:23:00 【过来我的小熊】
File类
- 概念:代表物理盘符中的一个文件或文件夹
- File类的使用
- 分隔符
- 文件操作
- 文件夹的操作
- 方法:
- createNewFile() 创建一个新文件
- mkdir() 创建一个新目录
- delete() 删除文件或空目录
- exists() 判断File对象所代表的对象是否存在
- getAbsolutePath() 获取文件的绝对路径
- getName() 取得名字
- getParent() 获取文件/目录所在的目录
- isDirectory() 是否是目录
- isFile() 是否是文件
- length() 获取文件的长度
- listFiles() 列出目录中的所有内容
- renameTo() 修改文件名为
File 类的使用
package com.io.file;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* File类的使用
* (1)分隔符
* (2)文件操作
* (3)文件夹操作
*/
public class Demo1 {
public static void main(String[] args) throws IOException, InterruptedException {
// separator();
// fileOpe();
directoryOpe();
}
// 分隔符
public static void separator(){
System.out.println("路径分隔符" + File.pathSeparator);
System.out.println("名称分隔符" + File.separator);
}
// 文件操作
public static void fileOpe() throws IOException, InterruptedException {
// 创建文件
File file = new File("E:\\桌面\\bbb.txt");
if (!file.exists()){
boolean b = file.createNewFile();
System.out.println("创建结果" + b);
}
// System.out.println(file.toString());
// 删除文件
// 直接删除
// System.out.println("删除结果" + file.delete());
// 使用jvm退出时删除
// file.deleteOnExit();
// Thread.sleep(3000); // 休眠时间 1000 = 1s
// 获取文件信息
// 获取文件的绝对路径
System.out.println("获取文件的绝对路径:" + file.getAbsolutePath());
System.out.println("获取路径:" + file.getPath());
System.out.println("获取名称:" + file.getName());
System.out.println("获取父目录:" + file.getParent());
System.out.println("获取文件长度:" + file.length());
System.out.println("获取创建时间:" + new Date(file.lastModified()).toLocaleString());
// 判断
System.out.println("是否可写:" + file.canWrite());
System.out.println("是否是文件:" + file.isFile());
System.out.println("是否是隐藏文件:" + file.isHidden());
}
// 文件夹操作
public static void directoryOpe() throws InterruptedException {
// 创建文件夹
File dir = new File("E:\\桌面\\aa\\bb");
System.out.println(dir.toString());
if (!dir.exists()){
// dir.mkdir(); // 只能创建单级目录
System.out.println("创建多级目录的结果:" + dir.mkdirs()); // 可以创建多级目录
}
// 删除文件夹
// 直接删除(只能删除最里层的空目录)
// System.out.println("删除结果为:" + dir.delete());
// 使用jvm删除
dir.deleteOnExit();
Thread.sleep(3000); // 休眠3s
// 获取文件夹信息
System.out.println("获取绝对路径:" + dir.getAbsolutePath());
System.out.println("获取路径:" + dir.getPath());
System.out.println("获取文件夹名称:" + dir.getName());
System.out.println("获取父目录:" + dir.getParent());
System.out.println("获取创建时间:" + new Date(dir.lastModified()).toLocaleString());
// 判断
System.out.println("是否为文件夹:" + dir.isDirectory());
System.out.println("是否是隐藏文件夹:" + dir.isHidden());
// 遍历文件夹 list() listFiles()
File dir1 = new File("E:\\桌面\\img");
// dir1.listFiles();
for (String s : dir1.list()) {
System.out.println(s);
}
}
}
边栏推荐
猜你喜欢
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
Read file by byte and character_load configuration file
绝了,这套RESTful API接口设计总结
运行flutter项目时遇到的问题
本体开发日记05-努力理解SWRL(中)
单元测试是什么?怎么写?主要测试什么?
本体开发日记03-理解代码
These 12 GIS software are better than the other
谷歌地图时代结束,怎么看高清卫星影像地图?
【百日行动】炎炎夏日安全不松懈 消防培训“加满”安全知识“油”
随机推荐
性能测试包括哪些方面?分类及测试方法有哪些?
秒拍app分析
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
seata处理分布式事务
How much do you know about the mobile APP testing process specifications and methods?
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
Amplify Shader Editor手册 Unity ASE(中文版)
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
MySQL transaction isolation
全球19级谷歌卫星地图免费查看下载
选择黑盒测试用例设计方法的综合策略方案总结
HD Satellite Map Browser
RPC服务远程漏洞
白盒测试的概念、目的是什么?及主要方法有哪些?
Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool
绝了,这套RESTful API接口设计总结
JS-常用方法整理
Read file by byte and character_load configuration file
软件测试流程包括哪些内容?测试方法有哪些?
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01