文章目录
概述
File
类位于java.io
包下。File
类的对象是文件或目录 路径名 的抽象表示。File
类的作用是 将文件或目录由硬盘加载到内存,由内存创建文件或目录到硬盘,或由内存删除硬盘上的文件或目录。
File
类的构造器
File(String pathname)
/* 通过将给定的路径名字符串转换为抽象路径名,创建一个新的文件实例。
注意:
1.File()不会检测pathname指定的路径格式的有效性;
2.pathname不能为null,否则将抛 NullPointerException;
3.pathname可以是""空字符串,此时File对象表示的抽象路径名为当前工程的路径名。 */
/* 该转换依赖于操作系统,转换结果在不同的操作系统上表现不同。 */
public File(String pathname){}
/* 当前工程目录为F:\Desktop\Code\ */
@Test
public void test(){
final String path_1 = "F:/Desktop/Test";
final String path_2 = "test.txt";
final String path_3 = "";
final String path_4 = "Wrong pathname*/-**/@#*/";
final String path_5 = null;
File file_1 = new File(path_1);
System.out.println(file_1.getAbsolutePath()); //F:\Desktop\Test
File file_2 = new File(path_2);
System.out.println(file_2.getAbsolutePath()); //F:\Desktop\Code\test.txt
File file_3 = new File(path_3);
System.out.println(file_3.getAbsolutePath()); //F:\Desktop\Code
File file_4 = new File(path_4);
System.out.println(file_4.getAbsolutePath()); //F:\Desktop\Code\Wrong pathname*\-**\@#*
File file_5 = new File(path_5);
System.out.println(file_5.getAbsolutePath()); //java.lang.NullPointerException
}
File(String parent,String child)
/* 通过将给定的父级路径字符串和子级路径字符串合成为抽象路径名,创建一个新的文件实例。
注意:
1.File()不会检测parent和child指定的路径格式的有效性;
2.parent可为null.child不可为null,否则将抛 NullPointerException;
parent和child均可为""空字符串:
(1)parent为null时,与调用File(String pathname)作用相同;
(2)parent不为null,child为""时,File对象表示的抽象路径名与parent表示的路径名一致;
(3)parent为""时,父级路径为当前工程所在驱动器的根路径。 */
/* 该转换依赖于操作系统,转换结果在不同的操作系统上表现不同。 */
public File(String parent,String child){}
/* 当前工程目录为F:\Desktop\Code\ */
@Test
public void test(){
final String parPath_1 = "F:/Desktop";
final String subPath_1 = "Test/test.txt";
final String parPath_2 = null;
final String subPath_2 = "test.txt";
final String parPath_3 = "F:/Desktop/Test";
final String subPath_3 = "";
final String parPath_4 = null;
final String subPath_4 = "";
final String parPath_5 = "";
final String subPath_5 = "test.txt";
final String parPath_6 = "";
final String subPath_6 = "";
File file_1 = new File(parPath_1,subPath_1);
System.out.println(file_1.getAbsolutePath()); //F:\Desktop\Test\test.txt
File file_2 = new File(parPath_2,subPath_2);
System.out.println(file_2.getAbsolutePath()); //F:\Desktop\Code\test.txt
File file_3 = new File(parPath_3,subPath_3);
System.out.println(file_3.getAbsolutePath()); //F:\Desktop\Test
File file_4 = new File(parPath_4,subPath_4);
System.out.println(file_4.getAbsolutePath()); //F:\Desktop\Code
File file_5 = new File(parPath_5,subPath_5);
System.out.println(file_5.getAbsolutePath()); //F:\test.txt
File file_6 = new File(parPath_6,subPath_6);
System.out.println(file_6.getAbsolutePath()); //F:\
}
File(File parent,String child)
/* 通过将给定的抽象父级路径名和子级路径字符串合成为抽象路径名,创建一个新的文件实例。
注意:
1.File()不会检测parent和child指定的路径格式的有效性;
2.parent可为null.child不可为null,否则将抛 NullPointerException;
parent表示的路径名和child均可为""空字符串:
(1)parent为null时,与调用File(String pathname)作用相同;
(2)parent不为null,child为""时,File对象表示的抽象路径名与parent表示的路径名一致;
(3)parent表示的路径名为""时,父级路径为当前工程所在驱动器的根路径。 */
/* 该转换依赖于操作系统,转换结果在不同的操作系统上表现不同。 */
public File(File parent,String child){}
/* 当前工程目录为F:\Desktop\Code\ */
@Test
public void test(){
final File parFile_1 = new File("F:/Desktop");
final String subPath_1 = "Test/test.txt";
final File parFile_2 = null;
final String subPath_2 = "test.txt";
final File parFile_3 = new File("F:/Desktop/Test");
final String subPath_3 = "";
final File parFile_4 = null;
final String subPath_4 = "";
final File parFile_5 = new File("");
final String subPath_5 = "test.txt";
final File parFile_6 = new File("");
final String subPath_6 = "";
File file_1 = new File(parFile_1,subPath_1);
System.out.println(file_1.getAbsolutePath()); //F:\Desktop\Test\test.txt
File file_2 = new File(parFile_2,subPath_2);
System.out.println(file_2.getAbsolutePath()); //F:\Desktop\Code\test.txt
File file_3 = new File(parFile_3,subPath_3);
System.out.println(file_3.getAbsolutePath()); //F:\Desktop\Test
File file_4 = new File(parFile_4,subPath_4);
System.out.println(file_4.getAbsolutePath()); //F:\Desktop\Code
File file_5 = new File(parFile_5,subPath_5);
System.out.println(file_5.getAbsolutePath()); //F:\test.txt
File file_6 = new File(parFile_6,subPath_6);
System.out.println(file_6.getAbsolutePath()); //F:\
}
File(URI uri)
/* 通过将给定文件的URI转换为抽象路径名,创建一个新的文件实例。 */
/* 该转换依赖于操作系统,转换结果在不同的操作系统上表现不同。 */
public File(URI uri){}
路径分隔符
DOS
系统(Windows
)和UNIX
系统(如Linux
)的路径分隔符有所不同,例如DOS
系统中路径的层级分隔符使用\
表示(\
也用来表示根目录),不同路径间的分隔符使用;
表示;UNIX
系统中路径的层级分隔符使用/
表示,不同路径间的分隔符使用:
表示。
注意:在Java程序中,可以使用/
和\\
表示Windows
系统中路径的层级分隔符,但不可使用\
表示,因为\
是语言基础中转义字符的组成部分。
为解决路径分隔符不统一的问题,Java提供了不依赖于操作系统的路径分隔符表示方式。
层级分隔符
/* char类型的文件路径的层级分隔符(常量字符) */
public static final char separatorChar
/* String类型的文件路径的层级分隔符(常量字符串) */
/* separator = "" + separatorChar; */
public static final String separator
@Test
public void test(){
String path = "F:" + File.separatorChar
+ "Desktop" + File.separator + "Test";
File file = new File(path);
String separator = File.separatorChar + File.separator;
System.out.println(separator); //\\
System.out.println(path); //F:\Desktop\Test
System.out.println(file.getAbsolutePath()); //F:\Desktop\Test
}
路径间分隔符
/* char类型的文件路径间分隔符(常量字符) */
public static final char pathSeparatorChar
/* String类型的文件路径间分隔符(常量字符串) */
/* pathSeparator = "" + pathSeparatorChar; */
public static final String pathSeparator
@Test
public void test(){
File file_1 = new File("F:" + File.separator
+ "Desktop" + File.separator + "File_1");
File file_2 = new File("F:" + File.separator
+ "Desktop" + File.separator + "File_2");
File file_3 = new File("F:" + File.separator
+ "Desktop" + File.separator + "File_3");
String paths = file_1.getAbsolutePath() + File.pathSeparatorChar
+ file_2.getAbsolutePath() + File.pathSeparator
+ file_3.getAbsolutePath();
String pathSeparator = File.pathSeparatorChar + File.pathSeparator;
System.out.println(pathSeparator); //;;
System.out.println(paths); //F:\Desktop\File_1;F:\Desktop\File_2;F:\Desktop\File_3
}
File
类中的常用方法
对于抽象路径名的操作
注意:File
类中对于抽象路径名的操作的方法不依赖于实际文件,而是针对File
对象所表示的抽象路径名。
/* 获得抽象路径名指向的文件名字符串。
(路径名指向的文件)指路径中的最内层文件或文件夹,例如:
Desktop\Test 中为 Test
Desktop\Test\test.txt 中为 test.txt */
public String getName(){}
/* 获得抽象路径名的路径名字符串 */
public String getPath(){}
/* 获得父级路径名字符串。
(父级路径)指路径中最内层文件或文件夹 以外 的路径,例如:
Desktop\Test 中为 Desktop
Desktop\Test\test.txt 中为 Desktop\Test */
public String getParent(){}
/* 获得文件的绝对路径名字符串。
(绝对路径)指带有盘符的完整路径,例如:
F:\Desktop\Test\test.txt */
/* 如果抽象路径名对应的路径原本就是绝对路径,
则调用 getAbsolutePath() 等价于调用 getPath(). */
public String getAbsolutePath(){}
/* 获得抽象路径名对应路径的父级抽象路径名对象 */
public File getParentFile(){}
/* 获得抽象路径名对应绝对路径的抽象路径名对象 */
public File getAbsoluteFile(){}
@Test
public void test(){
File file = new File("Test" + File.separator
+ "txt" + File.separator
+ "test_1.txt");
System.out.println(file.getName()); //test_1.txt
System.out.println(file.getPath()); //Test\txt\test_1.txt
System.out.println(file.getParent()); //Test\txt
System.out.println(file.getAbsolutePath()); //F:\Java\Code\Test\txt\test_1.txt
File file_1 = file.getParentFile();
System.out.println(file_1.getPath()); //Test\txt
File file_2 = file.getAbsoluteFile();
System.out.println(file_2.getPath()); //F:\Java\Code\Test\txt\test_1.txt
}
/* 获得抽象路径名对应路径的规范格式字符串。返回结果是 绝对路径 字符串。
先获得抽象路径名的 绝对路径,再对路径格式进行规范处理。*/
/* (规范格式)包括对冗余字符的删除、字母的大小写规范等。
getCanonicalPath()可根据操作系统对路径格式进行规范处理。 */
/* getCanonicalPath()会抛 IOException */
public String getCanonicalPath(){}
/* 获得抽象路径名对应路径的规范格式抽象路径名。返回结果是 绝对路径 抽象路径名对象。
先获得抽象路径名的 绝对路径,再对路径格式进行规范处理。*/
/* (规范格式)包括对冗余字符的删除、字母的大小写规范等。
getCanonicalFile()可根据操作系统对路径格式进行规范处理。 */
/* getCanonicalFile()会抛 IOException */
public File getCanonicalFile(){}
@Test
public void test(){
File file = new File("Test\\\\txt\\\\\\test.txt");
try{
/* getCanonicalPath()返回 绝对路径 字符串 */
System.out.println(file.getCanonicalPath());//F:\Java\Code\Test\txt\test.txt
File canonicalFile = file.getCanonicalFile();
/* getCanonicalFile()返回 绝对路径 File对象 */
System.out.println(canonicalFile.getPath());//F:\Java\Code\Test\txt\test.txt
} catch(IOException e) {
e.printStackTrace();
}
}
/* 判断抽象路径名所表示的是否为绝对路径 */
public boolean isAbsolute(){}
@Test
public void test(){
File file = new File("Test" + File.separator
+ "txt" + File.separator
+ "test.txt");
System.out.println(file.isAbsolute()); //false
System.out.println(file.getAbsoluteFile().isAbsolute()); //true
}
对于实际文件的操作
/* 根据抽象路径名指定的路径创建 一个 文件目录,
创建成功返回true,否则返回false. */
/* 创建失败的原因:
1.抽象路径名中包含 本地不存在 的目录(文件夹);
例如:F:\Desktop\Test\txt中Test目录本地不存在将导致创建目录失败。
2.目标目录本地已存在。*/
/* 注意:
如果抽象路径名指向一个具体文件,调用mkdir()会将其处理为文件目录(文件夹)。
例如:F:\Desktop\test.txt,mkdir()会将test.txt处理为文件夹。 */
public boolean mkdir(){}
/* 根据抽象路径名指定的路径创建 一个或多个 目录,即如果
路径中包含 本地不存在 的目录(文件夹),一并创建。
如果路径中不包含 本地不存在 的目录,作用与 mkdir() 相同。
创建成功返回true,否则返回false. */
/* 创建失败的原因:
目标目录本地已存在。*/
/* 注意:
如果抽象路径名指向一个具体文件,调用mkdir()会将其处理为文件目录(文件夹)。
例如:F:\Desktop\test.txt,mkdir()会将test.txt处理为文件夹。 */
public boolean mkdirs(){}
@Test
public void test(){
final String sp = File.separator;
boolean isSucceed;
File file_1 = new File("F:"+sp+"Desktop"+sp+"File_1");
File file_2 = new File("F:"+sp+"Desktop"+sp+"File_2"+sp+"Test");
File file_3 = new File(file_1,"test.txt");
isSucceed = file_1.mkdir();
//创建成功
if(isSucceed){
System.out.println("file_1 created succeed.");
} else {
System.out.println("file_1 created failed.");
}
isSucceed = file_1.mkdir();
//创建失败,因为目标路径本地已存在
if(isSucceed){
System.out.println("file_1 recreated succeed.");
} else {
System.out.println("file_1 recreated failed.");
}
isSucceed = file_2.mkdir();
//创建失败,因为路径中包含本地不存在的目录 File_2
if(isSucceed){
System.out.println("file_2 created succeed.");
} else {
System.out.println("file_2 created failed.");
}
isSucceed = file_2.mkdirs();
//创建成功
if(isSucceed){
System.out.println("file_2 created succeed.");
} else {
System.out.println("file_2 created failed.");
}
isSucceed = file_3.mkdir();
//创建成功
if(isSucceed){
System.out.println("file_3 created succeed.");
//test.txt的属性是 文件夹
if(file_3.isDirectory()){
System.out.println("file_3 is a directory.");
} else if(file_3.isFile()){
System.out.println("file_3 is a file.");
}
} else {
System.out.println("file_3 created failed.");
}
}
/* 根据抽象路径名指定的路径创建 文件。
创建成功返回true,否则返回false. */
/* 创建失败的原因:
1.目标文件本地已存在;
2.父级路径本地不存在(抛IOException)。*/
/* 如果目标路径指向表示目录,调用createNewFile()会将其处理为 具体文件。 */
/* createNewFile()会抛 IOException. */
public boolean createNewFile(){}
@Test
public void test(){
final String sp = File.separator;
boolean isSucceed;
File file_1 = new File("F:"+sp+"Desktop"+sp+"test.txt");
File file_2 = new File("F:"+sp+"Desktop"+sp+"test");
File file_3 = new File("F:"+sp+"Desktop"+sp+"Test"+sp+"test.txt");
try{
isSucceed = file_1.createNewFile();
//创建成功
if(isSucceed){System.out.println("file_1 created succeed.");}
else{System.out.println("file_1 created failed.");}
isSucceed = file_2.createNewFile();
//创建成功
if(isSucceed){
//test的属性是 具体文件
if(file_2.isDirectory()){System.out.println("file_2 is a directory.");}
else if(file_2.isFile()){System.out.println("file_2 is a file.");}
} else {System.out.println("file_2 created failed.");}
isSucceed = file_3.createNewFile();
//创建失败,抛 IOException,因为父级目录 Test 本地不存在
if(isSucceed){System.out.println("file_3 created succeed.");}
else{System.out.println("file_3 created failed.");}
} catch(IOException e) {
e.printStackTrace();
}
}
/* 判断抽象路径名指定的文件或目录是否本地已存在 */
public boolean exists(){}
/* 判断抽象路径名指定的是否为 具体文件。 */
/* 如果目标路径 本地不存在,isFile()将返回false,
此时可调用exists()进行判断,确切的说,
在调用isFile()之前应当首先调用exists(). */
public boolean isFile(){}
/* 判断抽象路径名指定的是否为 文件目录(文件夹)。 */
/* 如果目标路径 本地不存在,isDirectory()将返回false,
此时可调用exists()进行判断,确切的说,
在调用isDirectory()之前应当首先调用exists(). */
public boolean isDirectory(){}
/* 判断抽象路径名指向的文件或目录是否为 隐藏状态。 */
/* 如果目标路径 本地不存在,isHidden()将返回false,
此时可调用exists()进行判断,确切的说,
在调用isHidden()之前应当首先调用exists(). */
public boolean isHidden(){}
/* 获得指定文件或目录的最后修改时间。
如果指定文件或目录本地不存在,返回0. */
public long lastModified(){}
/* 判断抽象路径名指向的文件或目录是否 可读。 */
/* 如果目标路径 本地不存在,canRead()将返回false,
此时可调用exists()进行判断,确切的说,
在调用canRead()之前应当首先调用exists(). */
public boolean canRead(){}
/* 判断抽象路径名指向的文件或目录是否 可写入。 */
/* 如果目标路径 本地不存在,canWrite()将返回false,
此时可调用exists()进行判断,确切的说,
在调用canWrite()之前应当首先调用exists(). */
public boolean canWrite(){}
/* 设置指定文件或目录 是否可读。
设置成功返回true,否则返回false. */
/* 设置失败的原因:
1.目标文件本地不存在;
2.用户没有修改目标文件访问权限的权限;
3.底层文件系统没有实现读权限 */
/* 在调用setReadable()之前应当首先调用exists(). */
public boolean setReadable(boolean readable){}
/* 设置指定文件或目录 是否可写。
设置成功返回true,否则返回false. */
/* 设置失败的原因:
1.目标文件本地不存在;
2.用户没有修改目标文件访问权限的权限 */
/* 在调用setWritable()之前应当首先调用exists(). */
public boolean setWritable(boolean writable){}
/* 标记指定文件或目录为只读。
设置成功返回true,否则返回false. */
/* 设置失败的原因:
1.目标文件本地不存在;
2.用户没有修改目标文件访问权限的权限 */
/* 在调用setReadOnly()之前应当首先调用exists(). */
/* 注意:对于已设置只读的文件或目录,是否可以删除取决于操作系统。 */
public boolean setReadOnly(){}
/* 设置指定文件或目录的最后修改时间。
设置成功返回true,否则返回false. */
/* 失败的原因:指定的文件或目录本地不存在 */
/* 在调用setLastModified()之前应当首先调用exists(). */
public boolean setLastModified(long time){}
/* 删除指定文件或目录。删除成功返回true,否则返回false.
注意:删除目录时,只有指定目录内容为空,目录才能删除成功。 */
/* 删除失败的原因:
1.指定文件或目录本地不存在;
2.指定目录包含文件或目录;
3.用户没有删除权限 */
/* 注意:
使用delete()删除的文件或目录不会存在于回收站,而是直接删除 */
public boolean delete(){}
/* 在Java虚拟机终止时删除指定文件或目录。只有在正常终止虚拟机时才尝试删除。 */
/* 注意:
1.文件或目录的删除顺序与它们的注册顺序相反;
2.调用deleteOnExit()删除已注册删除的文件或目录是无效的;
3.一旦请求删除就不可能取消请求 */
public void deleteOnExit(){}
/* 通过重命名抽象路径名,重命名或移动指定文件或文件目录。
操作成功返回true,否则返回false. */
/* 操作失败的原因:
1.申请重命名的文件或目录本地不存在;
2.申请重命名的目标路径dest本地已存在;
3.申请重命名的目标路径dest的父级路径本地不存在,例如:申请将
F:\Desktop\File_1.test.txt 重命名为
F:\Desktop\File_2\test.txt,若父级路径
F:\Desktop\File_2本地不存在,则重命名失败。 */
/* 注意:
重命名目录时,如果目录中包含有文件或目录,依然可以重命名成功 */
public boolean renameTo(File dest){}
@Test
public void test(){
final String sp = File.separator;
boolean isSucceed;
File dir_1 = new File("F:"+sp+"Desktop"+sp+"dir_1");
File dir_2 = new File("F:"+sp+"Desktop"+sp+"dir_2");
File dir_3 = new File("F:"+sp+"Desktop"+sp+"dir_3");
File file_1 = new File(dir_1,"file_1.txt");
File file_2 = new File(dir_2,"file_2.txt");
File file_3 = new File(dir_3,"file_3.txt");
isSucceed = dir_1.mkdir();
if(isSucceed){
System.out.println("dir_1 created succeed.");
try{
isSucceed = file_1.createNewFile();
if(isSucceed) {System.out.println("file_1 created succeed.");}
else {System.out.println("file_1 created failed.");}
} catch(IOException e){e.printStackTrace();}
} else {System.out.println("dir_1 created failed.");}
isSucceed = dir_2.mkdir();
if(isSucceed){
System.out.println("dir_2 created succeed.");
try{
isSucceed = file_2.createNewFile();
if(isSucceed) {System.out.println("file_2 created succeed.");}
else {System.out.println("file_2 created failed.");}
} catch(IOException e){e.printStackTrace();}
} else {System.out.println("dir_2 created failed.");}
isSucceed = dir_1.delete();
//dir_1删除失败,因为dir_1中包含文件file_1.txt
if(isSucceed) {System.out.println("file_1 deleted succeed.");}
else {System.out.println("file_1 deleted failed.");}
isSucceed = dir_3.renameTo(new File("F:"+sp+"Desktop"+sp+"dir_4"));
//dir_3重命名失败,因为dir_3本地不存在
if(isSucceed) {System.out.println("dir_3 renamed succeed.");}
else {System.out.println("dir_3 renamed failed.");}
isSucceed = file_1.renameTo(file_2);
//file_1重命名失败,因为file_2本地已存在
if(isSucceed) {System.out.println("file_1 renamed succeed.");}
else {System.out.println("file_1 renamed failed.");}
isSucceed = file_1.renameTo(file_3);
//file_1重命名失败,因为file_3的父级目录dir_3本地不存在
if(isSucceed) {System.out.println("file_1 renamed succeed.");}
else {System.out.println("file_1 renamed failed.");}
isSucceed = file_1.renameTo(new File(dir_2,"file_1.txt"));
//file_1.txt移动到dir_2中,操作成功
if(isSucceed) {System.out.println("file_1 moved succeed.");}
else {System.out.println("file_1 moved failed.");}
isSucceed = dir_2.renameTo(dir_3);
/* dir_2中已包含file_1.txt和file_2.txt.
操作成功且file_1.txt和file_2.txt移动到dir_3中 */
if(isSucceed) {System.out.println("dir_2 renamed succeed.");}
else {System.out.println("dir_2 renamed failed.");}
}
/* 获得指定文件的字节长度,如果目标路径指向一个目录(文件夹),
返回0,即使该目录包含文件。 */
public long length(){}
/* 获得指定目录中包含的所有文件(或目录)的名字,
如果指定目录中不包含任何文件(或目录),或者指定的路径指向具体文件而并非目录,
返回null. */
/* 注意:
1.list()一次只能查询指定目录的根目录,不能更深层次的查询;
2.list()不能保证按照一定的顺序排布查询结果 */
public String[] list(){}
/* 获得指定目录中包含的所有文件(或目录)的抽象路径名对象,
如果指定目录中不包含任何文件(或目录),或者指定的路径指向具体文件而并非目录,
返回null. */
/* 注意:
1.listFiles()一次只能查询指定目录的根目录,不能更深层次的查询;
2.listFiles()不能保证按照一定的顺序排布查询结果 */
public File[] listFiles(){}
/* 某文件系统结构为:
F:Desktop
|--dir
|--file_1
|--readme.txt
|--index.html
|--file_2
|--Main.java
|--Main.class
|--file_3
*/
@Test
public void test(){
final String sp = File.separator;
File[] fileList;
File file_1 = new File("F:"+sp+"Desktop"+sp+"dir"+sp+"file_1");
File file_2 = new File("F:"+sp+"Desktop"+sp+"dir"+sp+"file_2");
File file_3 = new File("F:"+sp+"Desktop"+sp+"dir"+sp+"file_3");
if(file_1.exists()){
fileList = file_1.listFiles();
if(fileList != null && fileList.length > 0){
for(File f : fileList){
System.out.println(f.getAbsolutePath());
}
} else {System.out.println("There is no file in file_1");}
} else {System.out.println("Cannot find path "+file_1.getAbsolutePath());}
if(file_2.exists()){
fileList = file_2.listFiles();
if(fileList != null && fileList.length > 0){
for(File f : fileList){
System.out.println(f.getAbsolutePath());
}
} else {System.out.println("There is no file in file_2");}
} else {System.out.println("Cannot find path "+file_2.getAbsolutePath());}
if(file_3.exists()){
fileList = file_3.listFiles();
if(fileList != null && fileList.length > 0){
for(File f : fileList){
System.out.println(f.getAbsolutePath());
}
} else {System.out.println("There is no file in file_3");}
} else {System.out.println("Cannot find path "+file_3.getAbsolutePath());}
}
/* 运行结果:
F:\Desktop\dir\file_1\index.html
F:\Desktop\dir\file_1\readme.txt
F:\Desktop\dir\file_2\Main.class
F:\Desktop\dir\file_2\Main.java
There is no file in file_3
*/
路径过滤器
路径过滤器可以根据自定义目的过滤出符合要求的字符串路径名或抽象路径名。FilenameFilter
:字符串路径名过滤器FileFilter
:抽象路径名过滤器
File
类中,路径过滤器主要用于以下方法:
public String[] list(FilenameFilter filter){}
public File[] listFiles(FilenameFilter filter){}
public File[] listFiles(FileFilter filter){}
FilenameFilter
FilenameFilter
是java.io
包下的函数式接口,实现FilenameFilter
接口的类对象可作为字符串路径名过滤器。
FilenameFilter.java
public interface FilenameFilter {
boolean accept(File dir,String name);
}
调用具体方法(如listFiles()
)时,accept()
方法的参数dir
传入调用具体方法的File
对象本身(this
),参数name
遍历传入File
对象指定的路径根目录(目标文件列表)中的每个文件名(或目录名)字符串。根据重写accept(){}
指定 File
对象本身 与File
对象指定的路径 根目录(目标文件列表)中的文件(或目录) 的联系制定筛选机制。满足accept()
返回true
的目标文件列表中的文件(或目录)被过滤保留。
/* 某文件系统结构为:
F:\Desktop
|--java
|--Main.java
|--Main.class
|--Person.java
|--Person.class
|--Test.java
|--Test.class
*/
@Test
public void test(){
final String sp = File.separator;
File file = new File("F:"+sp+"Desktop"+sp+"java");
if(file.exists()){
File[] javaList = file.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir,String name){
//过滤保留所有名字包含"java"的文件
return name.contains(dir.getName());
}
});
File[] classList = file.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir,String name){
//过滤保留所有名字不包含"java"的文件
return !(name.contains(dir.getName()));
}
});
if(javaList != null && javaList.length > 0){
System.out.println("All java files :");
for(File f : javaList){
System.out.println(f.getAbsolutePath());
}
System.out.println("-------------------------");
} else {System.out.println("no matching file. ");}
if(classList != null && classList.length > 0){
System.out.println("All class files :");
for(File f : classList){
System.out.println(f.getAbsolutePath());
}
System.out.println("-------------------------");
} else {System.out.println("no matching file. ");}
} else {System.out.println("cannot find path "+file.getAbsolutePath()+".");}
}
运行结果:
All java files :
F:\Desktop\java\Main.java
F:\Desktop\java\Person.java
F:\Desktop\java\Test.java
-------------------------
All class files :
F:\Desktop\java\Main.class
F:\Desktop\java\Person.class
F:\Desktop\java\Test.class
-------------------------
FileFilter
FileFilter
是java.io
包下的函数式接口,实现FileFilter
接口的类对象可作为抽象路径名过滤器。
FileFilter.java
public interface FileFilter {
boolean accept(File pathname);
}
调用具体方法(如listFiles()
)时,accept()
方法的参数pathname
遍历传入调用具体方法的File
对象指定的路径根目录(目标文件列表)中的每一个文件(或目录)的File
对象。根据重写accept(){}
直接对目标文件列表中的文件(或目录)进行筛选。满足accept()
返回true
的目标文件列表中的文件(或目录)被过滤保留。
/* 某文件系统结构为:
F:\Desktop
|--java
|--Main.java
|--Main.class
|--Person.java
|--Person.class
|--Test.java
|--Test.class
*/
@Test
public void test(){
final String sp = File.separator;
File file = new File("F:"+sp+"Desktop"+sp+"java");
if(file.exists()){
File[] javaList = file.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname){
//过滤保留所有.java文件
return pathname.getName().contains(".java");
}
});
File[] classList = file.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname){
//过滤保留所有.class文件
return pathname.getName().contains(".class");
}
});
if(javaList != null && javaList.length > 0){
System.out.println("All java files :");
for(File f : javaList){
System.out.println(f.getAbsolutePath());
}
System.out.println("-------------------------");
} else {System.out.println("no matching file. ");}
if(classList != null && classList.length > 0){
System.out.println("All class files :");
for(File f : classList){
System.out.println(f.getAbsolutePath());
}
System.out.println("-------------------------");
} else {System.out.println("no matching file. ");}
} else {System.out.println("cannot find path "+file.getAbsolutePath()+".");}
}
运行结果:
All java files :
F:\Desktop\java\Main.java
F:\Desktop\java\Person.java
F:\Desktop\java\Test.java
-------------------------
All class files :
F:\Desktop\java\Main.class
F:\Desktop\java\Person.class
F:\Desktop\java\Test.class
-------------------------