title: File类
index_img: https://tuchuangs.com/imgs/2023/08/10/57e573eb0f04932e.png
tags:
- Java SE
categories:
- Java SE
hide: false
excerpt: File
构造方法
| 方法名称 | 说明 |
|---|---|
| public File(string pathname) | 根据文件路径创建文件对象 |
| public File(string parent, string child) | 根据父路径名字符串和子路径名字符串创建文件对象 |
| public File(File parent, string child) | 根据父路径对应文件对象和子路径名字符串创建文件对象 |
注意
-
File表示文件对象,可以是文件、文件夹
-
路径可以存在也可以不存在
-
需要注意路径分隔符的使用。
-
在使用这些方法时,需要注意文件路径的编码格式。
public class Main {
public static void main(String[] args) {
File file = new File("main\\java\bid\\simpleword\\file\\test.txt"); //根据路径获得文件对象
System.out.println(file);
File file1 = new File("main\\java", "simpleword\\file\\test.txt");
System.out.println(file1);
File file2 = new File("main\\java\bid\\simpleword\\file");
File file3 = new File(file2, "test.txt");
System.out.println(file3);
}
}

File的成员方法
判断获取
| 方法名称 | 说明 |
|---|---|
| public boolean isDirectory() | 判断此路径名表示的File是否为文件夹 |
| public boolean isFile() | 判断此路径名表示的File是否为文件 |
| public boolean exists() | 判断此路径名表示的File是否存在 |
| public long length() | 返回文件的大小(字节数量) |
| public string getAbsolutePath() | 返回文件的绝对路径 |
| public string getPath() | 返回定义文件时使用的路径 |
| public string getName() | 返回文件的名称,带后缀 |
| public long lastModified() | 返回文件的最后修改时间(时间毫秒值) |
public long length():只能用于文件,用于文件夹结果不准确。- 文件大小可以不断除
1024得到KB、M、G- 可以通过时间类将时间戳改为易读时间
public class Main {
public static void main(String[] args) {
File file = new File("src\\main\\java\\bid\\simpleword\\file\\test.txt");
File file1 = new File("src\\main\\java\\bid\\simpleword");
fileMsg(file);
fileMsg(file1);
}
public static void fileMsg(File file) {
if (file.exists()) {
int size = (int) Math.ceil(file.length() / 1024.0); //转换单位并向上取整
//这里写一个,写在外部,此处只是演示时间日期类
String time = turnTime(file);
String time1 = turnTime1(file);
if (file.isFile()) {
System.out.println("文件-绝对路径:[" + file.getAbsolutePath() + "]-文件名-[" + file.getName()
+ "]-文件大小-[" + size + "KB]-最后修改时间-[" + time + "]");
} else {
System.out.println("文件夹-绝对路径:[" + file.getAbsolutePath() + "]-文件夹名-[" + file.getName()
+ "]-最后修改时间-[" + time1 + "]");
}
} else {
System.out.println("文件路径-" + file.getPath() + "-无效!");
}
}
public static String turnTime(File file) {
//jdk8
Instant instant = Instant.ofEpochMilli(file.lastModified());
LocalDateTime time = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分").format(time);
}
public static String turnTime1(File file) {
//jdk7
Date date = new Date(file.lastModified());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
return simpleDateFormat.format(date);
}
}

创建删除
| 方法名称 | 说明 |
|---|---|
| public boolean createNewFile() | 创建一个新的空的文件【存在就创建失败】 |
| public boolean mkdir() | 创建单级文件夹 |
| public boolean mkdirs() | 创建多级文件夹【推荐】 |
| public boolean delete() | 删除文件、空文件夹 |
delete只能删除文件、空文件夹,且不走回收站。
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("src\\main\\java\\bid\\simpleword\\file\\my.txt"); //获取一个不存在的文件对象
File file1 = new File("src\\main\\java\\bid\\simpleword\\file\\my"); //获取一个不存在的文件对象
creatFile(file);
creatFile(file1);
}
public static void creatFile(File file) throws IOException {
if (file.createNewFile()) {
System.out.println("创建成功");
} else {
System.out.println("创建不成功");
}
}
/*
1.只有不存在才会成功
2.父类路径不许存在,否则异常
3.不加后缀名不会创建文件夹,会创建无后缀名的文件
*/
}


public class Main {
public static void main(String[] args) {
File file = new File("D:\\test\\java");
if (file.mkdir()) {
System.out.println("创建文件夹" + file.getName() + "成功"); //要想创建成功,前面的文件夹必须存在【这里不存在】
} else {
System.out.println("创建失败"); //存在或者前面的文件夹不存在
}
if (file.mkdirs()) {
System.out.println("创建文件夹" + file.getName() + "成功"); //前面的文件夹不窜在,自动创建
} else {
System.out.println("创建失败");
}
}
}


public class Main {
public static void main(String[] args) {
File file = new File("D:\\test"); //存在的非空目录
File file1 = new File("D:\\test\\tt.txt"); //存在的文件
File file2 = new File("D:\\test\\java"); //存在的空文件
deleteFile(file); //非空文件夹删不了
deleteFile(file1); //文件可以删
deleteFile(file2); //空文件夹可删
}
public static void deleteFile(File file) {
if (file.delete()) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
}

获取并遍历
| 方法名称 | 说明 |
|---|---|
| public File[] listFiles() | 获取当前该路径下所有内容(包含隐藏) |
- 不包含子目录,但是可以搭配递归找到所有文件和文件夹
- 权限不够,得到的是null
public class Main {
// 创建一个Map用于存储文件夹和其大小
static final Map<String, Long> dirSizeMap = new HashMap<>();
public static void main(String[] args) {
File root = new File("D:\\test");
long totalSize = calculateDirectorySize(root);
System.out.println("各文件夹大小kb: " + dirSizeMap);
System.out.println("总大小kb: " + totalSize);
}
public static long calculateDirectorySize(File directory) {
long length = 0;
if (directory.isFile()) {
length += directory.length();
} else {
for (File file : directory.listFiles()) {
if (file.isFile()) {
length += file.length();
} else {
length += calculateDirectorySize(file); //这里是关键,每次把下一次的与前面的相加
}
}
}
dirSizeMap.put(directory.getPath(), length); //每个文件夹和它的大小写入map集合
return length; //结束返回总大小
}
}
