File类
File 类常用 API 示例
准备工作
以下示例假设 C:/test/hello.txt 文件存在,内容随意。
1. getName() — 获取文件名
java
File f = new File("C:/test/hello.txt");
System.out.println(f.getName());输出:hello.txt2. getPath() — 获取路径
java
File f = new File("C:/test/hello.txt");
System.out.println(f.getPath());输出:C:/test/hello.txt3. getAbsolutePath() — 获取绝对路径
java
File f = new File("hello.txt"); // 相对路径
System.out.println(f.getAbsolutePath());输出:C:\Users\YourName\project\hello.txt4. getParent() — 获取父目录路径
java
File f = new File("C:/test/hello.txt");
System.out.println(f.getParent());输出:C:/test5. length() — 获取文件大小(字节)
java
File f = new File("C:/test/hello.txt");
System.out.println(f.length() + " 字节");输出:1024 字节6. exists() — 判断是否存在
java
File f1 = new File("C:/test/hello.txt");
File f2 = new File("C:/test/none.txt");
System.out.println(f1.exists());
System.out.println(f2.exists());输出:
true
false7. isFile() — 判断是否是文件
java
File f = new File("C:/test/hello.txt");
System.out.println(f.isFile());输出:true8. isDirectory() — 判断是否是目录
java
File dir = new File("C:/test");
File file = new File("C:/test/hello.txt");
System.out.println(dir.isDirectory());
System.out.println(file.isDirectory());输出:
true
false9. canRead() / canWrite() — 判断读写权限
java
File f = new File("C:/test/hello.txt");
System.out.println("可读:" + f.canRead());
System.out.println("可写:" + f.canWrite());输出:
可读:true
可写:true10. createNewFile() — 创建新文件
java
File f = new File("C:/test/new.txt");
if (!f.exists()) {
f.createNewFile();
System.out.println("创建成功:" + f.getName());
} else {
System.out.println("文件已存在");
}输出:创建成功:new.txt11. delete() — 删除文件
java
File f = new File("C:/test/new.txt");
if (f.delete()) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}输出:删除成功12. mkdir() — 创建单层目录
java
File dir = new File("C:/test/newDir");
if (dir.mkdir()) {
System.out.println("目录创建成功");
}输出:目录创建成功13. mkdirs() — 创建多层目录
java
File dir = new File("C:/test/a/b/c");
if (dir.mkdirs()) {
System.out.println("多层目录创建成功");
}输出:多层目录创建成功14. list() — 获取目录下所有文件名
java
File dir = new File("C:/test");
String[] names = dir.list();
for (String name : names) {
System.out.println(name);
}输出:
hello.txt
new.txt
newDir15. listFiles() — 获取目录下所有 File 对象
java
File dir = new File("C:/test");
File[] files = dir.listFiles();
for (File f : files) {
if (f.isDirectory()) {
System.out.println("📁 " + f.getName());
} else {
System.out.println("📄 " + f.getName());
}
}输出:
📄 hello.txt
📄 new.txt
📁 newDir16. renameTo() — 重命名文件
java
File oldFile = new File("C:/test/hello.txt");
File newFile = new File("C:/test/world.txt");
if (oldFile.renameTo(newFile)) {
System.out.println("重命名成功");
}输出:重命名成功API 速查表
| 方法 | 说明 | 返回值 |
|---|---|---|
getName() | 获取文件名 | String |
getPath() | 获取路径 | String |
getAbsolutePath() | 获取绝对路径 | String |
getParent() | 获取父目录路径 | String |
length() | 获取文件大小 | long |
exists() | 是否存在 | boolean |
isFile() | 是否是文件 | boolean |
isDirectory() | 是否是目录 | boolean |
canRead() | 是否可读 | boolean |
canWrite() | 是否可写 | boolean |
createNewFile() | 创建新文件 | boolean |
delete() | 删除文件 | boolean |
mkdir() | 创建单层目录 | boolean |
mkdirs() | 创建多层目录 | boolean |
list() | 获取文件名列表 | String[] |
listFiles() | 获取 File 对象列表 | File[] |
renameTo(File) | 重命名 | boolean |
