Skip to content

File类

File 类常用 API 示例

准备工作

以下示例假设 C:/test/hello.txt 文件存在,内容随意。


1. getName() — 获取文件名

java
File f = new File("C:/test/hello.txt");
System.out.println(f.getName());
输出:hello.txt

2. getPath() — 获取路径

java
File f = new File("C:/test/hello.txt");
System.out.println(f.getPath());
输出:C:/test/hello.txt

3. getAbsolutePath() — 获取绝对路径

java
File f = new File("hello.txt"); // 相对路径
System.out.println(f.getAbsolutePath());
输出:C:\Users\YourName\project\hello.txt

4. getParent() — 获取父目录路径

java
File f = new File("C:/test/hello.txt");
System.out.println(f.getParent());
输出:C:/test

5. 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
false

7. isFile() — 判断是否是文件

java
File f = new File("C:/test/hello.txt");
System.out.println(f.isFile());
输出:true

8. 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
false

9. canRead() / canWrite() — 判断读写权限

java
File f = new File("C:/test/hello.txt");
System.out.println("可读:" + f.canRead());
System.out.println("可写:" + f.canWrite());
输出:
可读:true
可写:true

10. 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.txt

11. 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
newDir

15. 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
📁 newDir

16. 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