简介
File
类是 Java 中用于表示文件和目录路径名的类,位于 java.io
包中。它提供了对文件和目录的操作方法,但并不代表实际的文件内容读写。File
类的主要功能是检查文件的存在、创建文件或目录、删除文件、获取文件信息等。
常用的 File
类方法
创建文件或目录
boolean createNewFile()
:创建一个新的文件,如果文件已存在,则返回 false
。
boolean mkdir()
:创建一个目录。
boolean mkdirs()
:创建多个目录(包括必要的但不存在的父目录)。
删除文件或目录
boolean delete()
:删除文件或目录。如果是目录,必须是空的。
检查文件/目录信息
boolean exists()
:检查文件或目录是否存在。
boolean isDirectory()
:判断该路径是否是目录。
boolean isFile()
:判断该路径是否是文件。
long length()
:返回文件的长度(以字节为单位),不能用于目录。
String getName()
:获取文件或目录的名称。
String getPath()
:获取文件或目录的路径。
String getAbsolutePath()
:获取文件或目录的绝对路径。
String getParent()
:获取父目录的路径。
读取文件列表
String[] list()
:列出目录中的文件和目录名。
String[] list(FilenameFilter filter)
:根据文件名过滤器返回列表。
File[] listFiles()
:列出目录中的文件和目录,以 File
对象数组的形式返回。
File[] listFiles(FileFilter filter)
:根据文件过滤器返回列表。
File[] listFiles(FilenameFilter filter)
:根据文件名过滤器返回列表。
文件重命名
boolean renameTo(File dest)
:重命名文件或目录。
文件权限
boolean canRead()
:判断是否可读。
boolean canWrite()
:判断是否可写。
boolean canExecute()
:判断是否可执行。
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import java.io.File; import java.io.IOException;
public class FileExample { public static void main(String[] args) { File file = new File("example.txt"); try { if (file.createNewFile()) { System.out.println("文件已创建: " + file.getName()); } else { System.out.println("文件已存在。"); } } catch (IOException e) { System.out.println("发生错误。"); e.printStackTrace(); }
if (file.exists()) { System.out.println("文件名: " + file.getName()); System.out.println("绝对路径: " + file.getAbsolutePath()); System.out.println("可写: " + file.canWrite()); System.out.println("可读: " + file.canRead()); System.out.println("文件大小: " + file.length() + " 字节"); } else { System.out.println("文件不存在。"); }
if (file.delete()) { System.out.println("文件已删除: " + file.getName()); } else { System.out.println("删除文件失败。"); } } }
|
FilenameFilter
FilenameFilter
是一个用于过滤目录中文件名的接口,通常与 File
类的 list()
或 listFiles()
方法配合使用。它的过滤条件基于文件的名称。
方法:
boolean accept(File dir, String name)
:这个方法用来定义过滤的逻辑。dir
表示当前的目录,name
是当前文件的名称。如果返回 true
,该文件将包含在过滤结果中。
示例代码(使用 FilenameFilter
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.io.File; import java.io.FilenameFilter;
public class FilenameFilterExample { public static void main(String[] args) { File dir = new File("example_directory");
FilenameFilter txtFilter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".txt"); } };
String[] files = dir.list(txtFilter); if (files != null && files.length > 0) { for (String file : files) { System.out.println(file); } } else { System.out.println("没有找到.txt文件。"); } } }
|
FileFilter
FileFilter
是一个用于过滤 File
对象的接口,通常与 File
类的 listFiles()
方法配合使用。与 FilenameFilter
不同,它直接过滤 File
对象,而不是仅基于文件名。
方法:
boolean accept(File pathname)
:这个方法用来定义过滤的逻辑。pathname
是当前的 File
对象。如果返回 true
,该文件将包含在过滤结果中。
示例代码(使用 FileFilter
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.io.File; import java.io.FileFilter;
public class FileFilterExample { public static void main(String[] args) { File dir = new File("example_directory");
FileFilter txtFileFilter = new FileFilter() { @Override public boolean accept(File file) { return file.isFile() && file.getName().endsWith(".txt"); } };
File[] files = dir.listFiles(txtFileFilter); if (files != null && files.length > 0) { for (File file : files) { System.out.println(file.getName()); } } else { System.out.println("没有找到.txt文件。"); } } }
|