java.io.File用于封装和平台无关的文件夹和文件夹对象。构建File对象时,没有要求对应的文件或者文件夹必须存在
构造器1: public File(String pathName)
- ```
例如
File file=new File("bbb.txt"); 参数是文件或者文件夹的路径,可以是绝对路径,也可以是相对路径
File ff = new File("c:\\windows\\win.ini"); //使用绝对路径指定一个具体的文件,注意\\和/的写法
File ff=new File("bbb/ccc.txt"); 相对路径,相对路径是从项目的根路径开始计算
//获取相对路径的起始点
File file = new File("");//参数为"."也可以
System.out.println(file.getAbsolutePath());
```
构造器2:public File(String parent, String child)
```
File ff = new File("c:\\windows\\win.ini");等价于File ff=new File("c:\\windows","win.ini")
```
常见方法:
- getAbsolutePath():String 获取绝对路径
- length():long 获取文件的大小,单位字节
- File[] files = file.listFiles(); 获取file对象的子对象
-
- isFile():boolean 判断文件对象是否为文件
- isDirectory():boolean 判断文件对象是否为文件夹
- exists():boolean 判断文件夹是否存在,存在true,不存在false
- mkdirs() 创建多级目录,例如d:/aa/bb/cc;mkdir()则只能创建一级,不能创建多级
- delete()可以删除文件或者文件夹,但是删除文件夹时要求文件夹为空
编码要求:
> 请遍历windows文件夹下的所有内容,如果是文件夹则输出绝对路径,如果是文件则输出文件大小
> 思路:由于无法提前预知选择嵌套次数,所以只能使用递归。但是注意:递归不是首选
```java
public class Main {
public static void main(String[] args) {
File file=new File("c:/windows");
show(file);
}
public static void show(File file) {
if (file.isDirectory()) {
System.out.println(file.getAbsolutePath());
File[] children = file.listFiles();
if (children != null && children.length > 0)
for (File child : children) {
show(child);
}
} else if (file.isFile())
System.out.println(file.getName() + "\t" + file.length());
}
}
```
编码要求:练习匿名内部类的用法
> 要求遍历windows下的所有后缀为.ini的文件,提示是windows下所有后代文件,不一定是子文件
>
> 匿名内部类和Lambda表达式【JDK1.8+支持函数式编程】
```java
public class Main1 {
public static void main(String[] args) {
File file=new File("c:/windows");
show(file);
}
public static void show(File file){
if(file.isDirectory()){
File[] children=file.listFiles();
if(children!=null && children.length>0){
for(File tmp:children)
show(tmp);
}
}else if(file.isFile()){
String fileName= file.getName();
if(fileName!=null && fileName.length()>0){
// int pos=fileName.indexOf(".");
int pos=fileName.lastIndexOf(".");
if(pos>0) {
String fileExt = fileName.substring(pos);
if(fileExt!=null && fileExt.length()>0){
if(".ini".equals(fileExt))
System.out.println(fileName);
}
}
}
}
}
}
```
实现方法2
> 遍历所使用的过滤器接口FilenameFilter和FileFliter
```java
public class Main2 {
public static void main(String[] args) {
File file = new File("c:/windows");
show(file);
}
public static void show(File file) {
if (file.isDirectory()) {
File[] children = file.listFiles(new FilenameFilter() { //匿名内部类
@Override
public boolean accept(File dir, String name) {
// dir是上层目录,name是需要判断的目录或者文件
// System.out.println(dir.getAbsolutePath());
// System.out.println(name);
File tmp = new File(dir, name);
if (tmp.isFile())
return name.endsWith(".ini"); // name.startsWith(".ini")
else
return true;
}
});
if (children != null && children.length > 0) {
for (File tmp : children)
show(tmp);
}
} else if (file.isFile()) {
System.out.println(file.getName());
}
}
}
```
更方便的写法:FileFilter
```java
public class Main3 {
public static void main(String[] args) {
File file = new File("c:/windows");
show(file);
}
public static void show(File file) {
if (file.isDirectory()) {
File[] children = file.listFiles(new FileFilter() { //匿名内部类
//两个Filter功能类似,只是回调方法的参数不一样,FileFilter比FilenameFilter简单一些,编程方便
public boolean accept(File pathname) {
if(pathname.isDirectory())
return true;
else if(pathname.isFile()){
String fileName= pathname.getName();
if(fileName.endsWith(".ini"))
return true;
}
return false;
}
});
if (children != null && children.length > 0) {
for (File tmp : children)
show(tmp);
}
} else if (file.isFile()) {
System.out.println(file.getName()+"\t"+file.length());
}
}
}
```
方法3:进一步简化编码,引入Lambda
```java
@FunctionalInterface 用于声明函数式接口
public interface FileFilter
```
具体编码实现
```java
public class Main4 {
public static void main(String[] args) {
File file = new File("c:/windows");
show(file);
}
public static void show(File file) {
if (file.isDirectory()) {
File[] children = file.listFiles(pathname -> { //lambda表达式
if (pathname.isDirectory())
return true;
else if (pathname.isFile()) {
String fileName = pathname.getName();
return fileName.endsWith(".ini");
}
return false;
});
if (children != null && children.length > 0) {
for (File tmp : children)
show(tmp);
}
} else if (file.isFile()) {
System.out.println(file.getName() + "\t" + file.length());
}
}
}