一、File类定义
1. File类主要是JAVA为文件这块的操作(如删除、新增等)而设计的相关类
2. File类的包名是java.io,其实现了Serializable, Comparable两大接口以便于其对象可序列化和比较
-
public class File implements Serializable,Comparable<File>
-
{
-
}
二、File类实例域
1) path :封装的String类型变量,代表了文件的路径
-
private String path;
-
public String getPath()
-
{
-
return path;
-
}
-
2) separatorChar:静态常量,代表了运行该程序的系统的路径分隔符,windows系统为"\" ,linux系统为“/”
-
public static void main(String[] args)
-
{
-
-
System.out.println(File.separator);
//在windows系统下运行结果为\
-
-
}
3) separator :静态常量,是由separatorChar扩展而来的字符串型常量,运行结果和separatorChar一样
public static final String separator = "" + separatorChar;
4)pathSeparatorChar :静态常量,代表用于分割多个路径的系统分隔符。 在UNIX系统上,此字符为':'而 Windows系统上它是';'
-
public static void main(String[] args)
-
{
-
-
System.out.println(File.pathSeparator);
//运行结果为;
-
}
5)pathSeparator: 静态常量,由pathSeparatorChar扩展而来的字符串,运行结果和pathSeparatorChar一样
public static final String pathSeparator = "" + pathSeparatorChar;
三、File类构造函数
注意:构造函数只是创建一个File实例,并没有以文件做读取等操作,因此路径即使是错误的,也可以创建实例不报错
1)通过给定的字符串路径(一般是文件的绝对路径)转为抽象路径名用来创建File实例,当传入null时会抛出NullPointerException空异常错误
public File(String pathname) {}
-
public class Test2
-
{
-
-
public static void main(String[] args)
-
{
-
File file=
new File(
"xxx");
-
-
System.out.println(file.getPath());
//路径结果就是xxx
-
}
-
}
2)从父路径名字符串和子路径名字符串(一般是相对父类的相对路径)创建新的File实例
public File(String parent, String child) {}
2.1) 若子路径child为Null,会抛出NullPointerException空异常错误
2.2) 当父路径为Null时,会以子路径child作为绝对路径创建实例,等同于调用第一个File(String child )效果一样
-
public class Test2
-
{
-
-
public static void main(String[] args)
-
{
-
String parent=
null;
-
-
File file=
new File(parent,
"xxx");
-
-
System.out.println(file.getPath());
//路径结果就是xxx
-
}
-
}
2.3) 当父路径不为空时,会以父路径作为目录,子路径作为父路径下的目录或者文件名,最后得到的实例对象的路径就是父路径和子路径的组合
-
public class Test2
-
{
-
-
public static void main(String[] args)
-
{
-
String parent =
"E:/test";
-
-
File parentFile=
new File(parent);
-
-
String child =
"E:/test/1.txt";
-
-
File file =
new File(parentFile, child);
-
-
System.out.println(file.getPath());
// 路径结果就是E:\test\E:\test\1.txt
-
}
-
}
3)通过父路径File实例对象和子路径字符串创建新的File实例,等同于上面的方法中把父路径字符串创建File实例然后传入一样
-
public class Test2
-
{
-
-
public static void main(String[] args)
-
{
-
String parent =
"E:/test";
-
-
File parentFile=
new File(parent);
-
-
String child =
"E:/test/1.txt";
-
-
File file =
new File(parentFile, child);
-
-
System.out.println(file.getPath());
// 路径结果就是E:\test\E:\test\1.txt
-
}
-
}
4)通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例,这个方法暂时没有看懂,需要查阅资料加深理解
public File(URI uri) {}
四、File类中的各种常用方法
下面是我E盘下test文件夹的文件情况,以此作为案例
1)获取实例对象代表的文件名字(包含文件后缀)
public String getName()}
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.getName());
// 结果是1.txt
-
-
System.out.println(file2.getName());
// 结果是异常1.jpg
-
-
System.out.println(file3.getName());
// 结果是xxx
-
-
}
-
}
2)获取实例对象代表的文件上级目录
public String getParent()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.getParent());
// 结果是E:\test
-
-
System.out.println(file2.getParent());
// 结果是E:\test
-
-
System.out.println(file3.getParent());
// 结果是null
-
-
}
-
}
-
3)获取实例对象的父项的实例对象,如果此路径名未指定父目录,则返回null;也就是获取对象的上级目录然后再实例化一个对象
public File getParentFile()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.getParentFile());
// 结果是E:\test
-
-
System.out.println(file2.getParentFile());
// 结果是E:\test
-
-
System.out.println(file3.getParentFile());
// 结果是null
-
-
}
-
}
4)获取实例对象代表的文件的实际路径
public String getPath()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.getPath());
// 结果是E:\test\1.txt
-
-
System.out.println(file2.getPath());
// 结果是E:\test\异常1.jpg
-
-
System.out.println(file3.getPath());
// 结果是xxx
-
-
}
-
}
-
5)检测该实例对象代表的文件的路径是否是绝对路径(windows系统中路径是以驱动盘开始的就是绝对路径)
public boolean isAbsolute()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.isAbsolute());
// 结果是true
-
-
System.out.println(file2.isAbsolute());
// 结果是true
-
-
System.out.println(file3.isAbsolute());
// 结果是false
-
-
}
-
}
-
6)获取实例对象代表的文件的绝对路径
public String getAbsolutePath()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.getAbsolutePath());
// 结果是E:\test\1.txt
-
-
System.out.println(file2.getAbsolutePath());
// 结果是E:\test\异常1.jpg
-
-
System.out.println(file3.getAbsolutePath());
// 结果是D:\workspace\lettcode\xxx(编译器的工作空间作为解析目录了)
-
-
}
-
}
7)实例对象代表的文件是否存在
public boolean exists()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.exists());
// 结果是true
-
-
System.out.println(file2.exists());
// 结果是true
-
-
System.out.println(file3.exists());
// 结果是false
-
-
}
-
}
8)检测实例对象代表的是否是文件
public boolean isFile()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.isFile());
// 结果是true
-
-
System.out.println(file2.isFile());
// 结果是true
-
-
System.out.println(file3.isFile());
// 结果是false
-
-
}
-
}
9)检测实例对象代表的是否是目录
public boolean isDirectory()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws URISyntaxException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.isDirectory());
// 结果是false
-
-
System.out.println(file2.isDirectory());
// 结果是false
-
-
System.out.println(file3.isDirectory());
// 结果是false
-
-
}
-
}
10)创建新文件--当且仅当实例对象代表的文件不存在时才可以创建新文件
public boolean createNewFile() throws IOException
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file =
new File(
"E:/test/1.txt");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.createNewFile());
// 结果是false,已经存在无法创建
-
-
System.out.println(file2.createNewFile());
// 结果是false,已经存在无法创建
-
-
System.out.println(file3.createNewFile());
// 结果是true,不存在可以创建
-
-
}
-
}
-
创建的XXX文件的目录地址是以编译器的工作空间为上级目录加上创建实例对象的时候的路径名形成最后的路径
11)删除实例对象代表的文件或目录,当代表目录时,必须目录下为空才可以删除
public boolean delete()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file =
new File(
"E:/test/");
-
-
File file2 =
new File(
"E:/test/异常1.jpg");
-
-
File file3 =
new File(
"xxx");
// 错误路径
-
-
System.out.println(file.delete());
// 结果是false,目录无法删除
-
-
System.out.println(file2.delete());
// 结果是true,文件可以删除
-
-
System.out.println(file3.delete());
// 结果是true,文件可以删除
-
-
}
-
}
-
执行方法后的test文件如下:
12)根据实例对象的路径名创建目录(若目录已存在,则false;若路径是文件,则fasle;若路径的上级目录不存在则false)
public boolean mkdir()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file =
new File(
"E:/test1/");
-
-
File file2 =
new File(
"E:/test2/异常1.jpg");
-
-
File file3 =
new File(
"E:/ceshi");
-
-
File file4 =
new File(
"E:/ceshi2/2018");
-
-
System.out.println(file.mkdir());
// false 因为test1目录已存在
-
-
System.out.println(file2.mkdir());
// false,因为上级目录test2不存在
-
-
System.out.println(file3.mkdir());
// true,因为ceshi目录不存在且是上级目录是E盘存在的
-
-
System.out.println(file4.mkdir());
// false,因为ceshi2目录不存在,所以不成功
-
-
}
-
}
13)根据实例对象的路径创建目录,包括创建那些必须的且不存在的父级目录
public boolean mkdirs()
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file =
new File(
"E:/test1/");
-
-
File file2 =
new File(
"E:/test2/异常1.jpg");
-
-
File file3 =
new File(
"E:/ceshi");
-
-
File file4 =
new File(
"E:/ceshi2/zhagnsan");
-
-
System.out.println(file.mkdirs());
// false 因为test1目录已存在
-
-
System.out.println(file2.mkdirs());
// true,因为目录不存在所以可以创建,但注意生成的异常1.jpg是文件夹不是图片
-
-
System.out.println(file3.mkdirs());
// true,因为目录不存在所以可以创建
-
-
System.out.println(file4.mkdirs());
// true,因为目录不存在所以可以创建
-
-
}
-
}
-
14) 获取实例对象代表的文件下的各级文件名和目录名,返回一个字符串数组
public String[] list()
1. 当实例对象代表的是文件不是目录时,返回NUll对象
2. 获取的是该目录下的文件名和目录名,并不包含该目录名称和其上级目录名称
3. 字符串数组中都是文件名或目录名并不是路径名
4. 字符串中的元素并不是按照实际系统中目录下的顺序排列的
例如E盘test文件夹里面是这样的
调用list方法
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file2 =
new File(
"E:/test");
-
-
String[] list = file2.list();
-
-
for (
int i =
0; i < list.length; i++)
-
{
-
System.out.println(list[i]);
// 结果为1.txt、xxx、工资表.xls、异常1.jpg、第一个目录、第三个目录
-
}
-
-
}
-
}
15)获取实例对象代表的文件下的各级文件名和目录名并指定过滤器进行过滤,返回一个字符串数组
public String[] list(FilenameFilter filter)
1. 过滤器是FilenameFilter类对象,当传入null时,效果和list()方法一样
2. 过滤器是指过滤掉不符合名称的名字
3. FilenameFilter 是一个接口,因此需要自己新建一个类来实现该接口作为参数传入进去
4.仔细研究方法的源码可以发现,所谓过滤就是要重写FilenameFilter的accept方法并在方法中过滤
-
public String[] list(FilenameFilter filter) {
-
String names[] = list();
-
if ((names ==
null) || (filter ==
null)) {
-
return names;
-
}
-
List<String> v =
new ArrayList<>();
-
for (
int i =
0 ; i < names.length ; i++) {
-
if (filter.accept(
this, names[i])) {
//必须重写accept方法,并在方法内对传入的name进行判定是否合乎过滤条件
-
v.add(names[i]);
-
}
-
}
-
return v.toArray(
new String[v.size()]);
-
}
-
5. 例如我们只要后缀名是txt的文件名称,那么可以按照以下逻辑来处理
创建FilenameFilter的实现类TxtFilter并重写accept方法,在方法中进行判定
-
public class TxtFilter implements FilenameFilter
-
{
-
-
@Override
-
public boolean accept(File dir, String name)
-
{
-
if(name.endsWith(
".txt"))
-
{
-
return
true;
-
}
-
return
false;
-
}
-
-
}
但是仔细思考,如果我还需要过滤.java结束的呢?难道又去重新创建实现类?因此可以用过滤条件作为构造参数传入构造方法中初始化其变量,这样就是可变的
-
public class TxtFilter implements FilenameFilter
-
{
-
-
private String FilterName;
// 作为过滤器名称变量
-
-
public TxtFilter(String FilterName)
-
{
-
-
this.FilterName = FilterName;
// 初始化构造过滤器名称,这样就不用每次都创建新类
-
-
}
-
-
public String getFilterName()
-
{
-
-
return FilterName;
// 公有的域访问器方法,提供接口获取
-
}
-
-
@Override
-
public boolean accept(File dir, String name)
-
{
-
-
String fileterNameString =
this.getFilterName();
// this代表着调用accept方法的变量
-
-
if (name.endsWith(fileterNameString))
-
{
-
return
true;
-
}
-
return
false;
-
}
-
-
}
我们测试下结果
-
public class Test2
-
{
-
-
public static void main(String[] args) throws IOException
-
{
-
-
File file2 =
new File(
"E:/test");
-
-
TxtFilter filter=
new TxtFilter(
".txt");
//过滤器,只需要.txt结尾的
-
-
String[] list = file2.list(filter);
-
-
for (
int i =
0; i < list.length; i++)
-
{
-
System.out.println(list[i]);
// 结果为1.txt
-
}
-
-
}
-
}