
正文
Java 文件类 File
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.File 类
1.File 类1.1.构造方法- 文件的 抽象路径名(操作系统无关)
- 文件的 抽象路径名(操作系统无关)
| 格式 | 说明 |
| File(String filename) | 把文件路径名字符串转换为“抽象路径名”,用来 创建 File实例。 filename 为空,报异常“空指针” |
| File(String parent, String filename) | 通过两个路径名字符串拼接后,创建 File 实例。当 filename 为空时,报异常,parent为空效果与上边一致 |
| File(File parent, String filename) | 依据抽象路径名parent和filename,创建对象。 |
创建指向filename的一个抽象路径名。
String filename = "a.txt";
String parent = "dir1"; File file = new File(parent);
File file2 = new File(parent, filename);
File file3 = new File(file, filename);
对象创建后,这个filename指向的文件是普通文件、是目录、或者根本不存在,好像没有啥影响。
1.2.常用方法- File 类的意义
- 创建
- 删除
- 获取
- 判断
- 创建
- 删除
- 获取
- 判断
格式 | 说明 | |
| 创建 | boolean createNewFile() | 把该抽象路径名,创建为普通文件 |
| boolean mkdir() | 把该抽象路径名,创建为目录文件(# mkdir itheima) | |
| boolean mkdirs() | 父目录不存在的情况下,一并创建多个抽象路径名的对象(# mkdir -p xian/JavaEE25/itheima) | |
| 删除 | boolean delete() | 删除该抽象路径名指向的文件。成功删除时,返回 true |
| 获取 | long length() | 返回该抽象路径名指向的文件的大小。实例1.1 |
| String getAbsolutePath() | 实例1.2,获取该抽象路径名对象的绝对路径名 | |
| File getAbsoluteFile() | 实例1.3 | |
| long getFreeSpace() | 返回该抽象路径名指向的分区 可用空间 | |
| long getTotalSpace() | 返回……分区大小 | |
| String getPath() | 返回 抽象路径名 的路径名 | |
| String getName() | 返回 抽象路径名 的文件名 | |
| String getParent() | 返回 抽象路径名 的父目录名 | |
| File getParentFile() | 返回 抽象路径名 的父目录的 “抽象路径名对象” | |
| 判断 | boolean exists() | 返回该抽象路径名在系统中的存在状态。存在时,返回 true |
| boolean isFile() | 返回该抽象路径名的文件状态。普通文件,返回 true | |
| boolean isDirectory() | 返回该抽象路径名的文件状态。目录文件,返回 true | |
| boolean isAbsolute() | 该抽象路径名是否 指向一个绝对路径的文件。实例1.3 |
- 实例 1.1.当抽象路径名指向为空时,返回的 length() 结果是0。
String filename = "a.txt";
String parent = "dir1"; File file = new File(parent);
File file2 = new File(parent, filename);
File file3 = new File(file, filename); System.out.println("length = " + file3.length()); // 指向为空,0
file.mkdir();
System.out.println(file.length()); //
file2.createNewFile();
System.out.println(file2.length()); //
- 实例 1.2.获取抽象路径名对象的 “绝对路径名”
File file = new File("itheima.txt");
file.createNewFile(); // 返回:E:\eclipse_workspace\ItheimaAdvance_Review\itheima.txt
System.out.println(file.getAbsolutePath());
- 实例 1.3.指向 绝对路径名 的抽象路径名对象
File fileAbs = file.getAbsoluteFile();
System.out.println(file.isAbsolute()); // false
System.out.println(fileAbs.isAbsolute()); // true
- 实例 1.4.文件即使不存在,也不影响下边实例的输出结果。
File file = new File("E:\\itheima.txt");
file.createNewFile(); System.out.println(file.getPath()); // E:\itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // E:\
File file = new File("itheima.txt");
file.createNewFile(); System.out.println(file.getPath()); // itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // null
File file = new File("\\000\\itheima.txt");
// file.createNewFile(); System.out.println(file.getPath()); // \000\itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // \000
返回的结果,受到创建 “抽象路径名” 对象时定义的影响。获取 父目录、父目录抽象路径名 时,可能为 null。
1.3.常用方法2
格式 | 说明 |
| String[] list() | 该抽象路径名 指向的目录下的 文件名 |
| File[] listFiles() | 该抽象路径名 指向的目录,目录下的文件的抽象路径名 |
- 分别返回 字符串、抽象路径名对象。
public class Demo5 { public static void main(String[] args) throws IOException { String dir = "000";
File directory = new File(dir); // 生成一个目录
if (!directory.exists()) {
directory.mkdir();
}
// 在目录下创建三个普通文件
for (int i = 0; i < 3; i++) {
File file = new File(directory, "itheima" + i + ".txt"); // 局部变量
if (!file.exists()) {
file.createNewFile();
}
} // 抽象路径名file指向的目录下的文件名
String[] file = directory.list();
for (String f : file) {
System.out.println(f);
}
// 直接返回了字符串,只能直接输出了
// itheima0.txt
// itheima1.txt
// itheima2.txt // 抽象路径名file指向的目录,目录中文件的抽象路径名
File[] files = directory.listFiles();
for (File f : files) {
System.out.println(f.getName() + " - " + f.getPath());
}
// 输出结果,因为是个File类,可以做的事情比较多
// itheima0.txt - 000\itheima0.txt
// itheima1.txt - 000\itheima1.txt
// itheima2.txt - 000\itheima2.txt
}
}
1.4.获取目录结构- 目的:使用递归,打印指定的文件名构建的 抽象路径名对象 的目录结构
- 代码:
public class Demo7 { public static void main(String[] args) throws IOException { String dirName = "000"; // 存在
// String dirName = "111"; // 不存在
// String dirName = "000\\itheima0.txt"; // 存在
// String dirName = "000\\itheima000.txt"; // 不存在
File file = new File(dirName);
ls_R(file); } private static void ls_R(File file) {
String indentTab = "";
ls_R(file, indentTab); // 打印目录结构时,下一级缩进
} private static void ls_R(File file, String indentTab) {
if (file.isFile()) {
System.out.println(indentTab + file.getName());
// System.out.println(indentTab + file.getPath());
} else if (file.isDirectory()) {
System.out.println(indentTab + file.getPath());
indentTab = indentTab + "\t";
File[] tmp = file.listFiles();
for (File f : tmp) {
ls_R(f, indentTab);
}
} else {
System.out.println("抽象路径名对象不存在!");
}
}
}
- 输出结果:
000
000\111
itheima0.txt
itheima1.txt
itheima2.txt
000\222
000\222\333
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt
1.5.模拟下linux下删除目录- 模拟Linux下 “rm -frv” 命令的显示效果,代码:
public class Demo9 { public static void main(String[] args) throws IOException { String dirName = "000"; // 存在
File file = new File(dirName);
rm_R(file); } private static void rm_R(File file) {
if (file.isFile()) {
// System.out.println("文件正在删除..." + file.getPath());
} else if (file.isDirectory()) {
// System.out.println("目录正在删除..." + file.getPath());
File[] tmp = file.listFiles();
for (File f : tmp) {
rm_R(f);
}
} else {
System.out.println("抽象路径名对象不存在!");
} // 修改成Linux下“rm -frv 000”的现实效果
if (file.isDirectory())
System.out.println("removed directory: '" + file.getPath() + "'");
else if (file.isFile())
System.out.println("removed '" + file.getPath() + "'");
file.delete(); // 删除目录文件、普通文件 }
}
public class Demo7 { public static void main(String[] args) throws IOException { String dirName = "000"; // 存在
// String dirName = "111"; // 不存在
// String dirName = "000\\itheima0.txt"; // 存在
// String dirName = "000\\itheima000.txt"; // 不存在
File file = new File(dirName);
ls_R(file); } private static void ls_R(File file) {
String indentTab = "";
ls_R(file, indentTab); // 打印目录结构时,下一级缩进
} private static void ls_R(File file, String indentTab) {
if (file.isFile()) {
System.out.println(indentTab + file.getName());
// System.out.println(indentTab + file.getPath());
} else if (file.isDirectory()) {
System.out.println(indentTab + file.getPath());
indentTab = indentTab + "\t";
File[] tmp = file.listFiles();
for (File f : tmp) {
ls_R(f, indentTab);
}
} else {
System.out.println("抽象路径名对象不存在!");
}
}
}
000
000\111
itheima0.txt
itheima1.txt
itheima2.txt
000\222
000\222\333
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt
- 模拟Linux下 “rm -frv” 命令的显示效果,代码:
public class Demo9 { public static void main(String[] args) throws IOException { String dirName = "000"; // 存在
File file = new File(dirName);
rm_R(file); } private static void rm_R(File file) {
if (file.isFile()) {
// System.out.println("文件正在删除..." + file.getPath());
} else if (file.isDirectory()) {
// System.out.println("目录正在删除..." + file.getPath());
File[] tmp = file.listFiles();
for (File f : tmp) {
rm_R(f);
}
} else {
System.out.println("抽象路径名对象不存在!");
} // 修改成Linux下“rm -frv 000”的现实效果
if (file.isDirectory())
System.out.println("removed directory: '" + file.getPath() + "'");
else if (file.isFile())
System.out.println("removed '" + file.getPath() + "'");
file.delete(); // 删除目录文件、普通文件 }
}
效果:
[argor@donatello day10 File]$ java Demo9
removed '000/111/itheima2.txt'
removed '000/111/itheima0.txt'
removed '000/111/itheima1.txt'
removed directory: '000/111'
removed '000/itheima2.txt'
removed '000/itheima0.txt'
removed '000/itheima1.txt'
removed '000/222/itheima2.txt'
removed '000/222/itheima0.txt'
removed '000/222/333/itheima2.txt'
removed '000/222/333/itheima0.txt'
removed '000/222/333/itheima1.txt'
removed directory: '000/222/333'
removed '000/222/itheima1.txt'
removed directory: '000/222'
removed directory: ''
[argor@donatello day10 File]$ rm -frv
removed ‘//itheima2.txt’
removed ‘//itheima0.txt’
removed ‘//itheima1.txt’
removed directory: ‘/’
removed ‘/itheima2.txt’
removed ‘/itheima0.txt’
removed ‘/itheima1.txt’
removed ‘//itheima2.txt’
removed ‘//itheima0.txt’
removed ‘///itheima2.txt’
removed ‘///itheima0.txt’
removed ‘///itheima1.txt’
removed directory: ‘//’
removed ‘//itheima1.txt’
removed directory: ‘/’
removed directory: ‘’
A






