1、介绍
File类是java.io包下代表与平台无关的文件和目录,也就是说,如果希望在程序中操作文件和目录,都可以通过File类来完成。不管是文件还是目录都是使用File来操作的,File能新建、删除、重命名文件和目录,File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
2.静态变量
public class FileDemo { public static void main(String[] args) { // File类静态成员变量 // 与系统有关的路径分割符 String separator = File.pathSeparator; System.out.println(separator); // 输出结果为‘;’ // 与系统有关的默认名称分割符 separator = File.separator; System.out.println(separator); // 输出结果为‘\’ } }
3、构造方法
· public File(String pathname):根据一个路径得到File对象
· public File(String parent,String child):根据一个目录和一个子文件/目录得到File对象
· public File(File parent,String child):根据一个父File对象和一个子文件/目录得到File对象
构造方法一:public File(String pathname)
这里的pathname可以是文件名,也可以是文件夹
代码:

1 // 文件夹 2 public class FileConstructor { 3 public static void main(String[] args){ 4 File file = new File("F://Code"); 5 System.out.println(file); 6 } 7 }
输出结果:F:\Code
代码:
// 文件名 public class FileConstructor { public static void main(String[] args){ File file = new File("F://Code//123.txt"); System.out.println(file); } }
输出结果: F:\Code\123.txt
构造方法二:public File(String parent,String child)
代码:
public class FileConstructor { public static void main(String[] args){ File file = new File("F:", "Code"); System.out.println(file); } }
输出结果:F:\Code
构造方法三:public File(File parent,String child)
代码:
public class FileConstructor { public static void main(String[] args){ File parent = new File("F:"); File file = new File(parent, "Code"); System.out.println(file); } }
输出结果:F:\Code
4.File类常用方法
方法1.boolean createNewFile()方法:用于创建文件
注意:只能用来创建文件,而无法创建文件夹。
代码:
public class FileConstructor { public static void main(String[] args) throws IOException { // 创建的文件名和文件路径,在File的构造方法中给出 File file = new File("F:\\123.txt"); boolean b = file.createNewFile(); System.out.println(b); } }
注:当在F盘下不存在123.txt文件时,调用 createNewFile() 将会创建123.txt,并返回一个布尔值:true;当F盘下已经存在123.txt文件时,调用createNewFile()方法将会返回false。
方法2.boolean mkdir()方法:用于创建文件夹
创建的路径在File的构造方法中给出
如果文件夹已存在,则不再创建
只能创建一层文件夹
代码:

1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123"); 4 boolean b = file.mkdir(); 5 System.out.println(b); 6 } 7 }
文件夹不存在:输出结果为 true
文件夹已存在:输出结果为 false
方法3.boolean mkdirs():用于创建文件夹
同mkdir()类似,只是该方法可以创建多层文件夹,推荐使用该方法
代码:

1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123\\a\\b\\c"); 4 boolean b = file.mkdirs(); 5 System.out.println(b); 6 } 7 }
方法4.boolean delete():用于删除文件或文件夹
删除的文件或者文件夹,在File类的构造函数中给出
删除成功返回true,删除失败返回false
删除方法,不走回收站,直接从硬盘中删除
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123\\a.txt"); 4 boolean newFile = file.delete(); 5 System.out.println(newFile); 6 } 7 }
方法5.String getName():返回路径中表示的文件名或者文件夹名
获取路径中最后部分的名字
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123\\a.txt"); 4 System.out.println(file.getName()); 5 } 6 }
输出结果:a.txt
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123"); 4 System.out.println(file.getName()); 5 } 6 }
输出结果:123
方法6:long length():返回路径中表示的文件的字节数
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123\\123.txt"); 4 System.out.println(file.length()); 5 } 6 }
输出文件123.txt的字节数
方法7:String getAbsolutePath():获取文件的绝对路径,返回String对象
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("123.txt"); 4 System.out.println(file.getAbsolutePath()); 5 } 6 }
输出结果:D:\JavaCode\cn.itcast.demo\123.txt
File对象中写的是一个相对路径,相对于当前项目而言,当前项目的绝对路径
方法8:File getAbsoluteFile():获取文件的绝对路径,返回File对象
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123.txt"); 4 System.out.println(file.getAbsoluteFile()); 5 } 6 }
输出结果:F:\123.txt
方法9:String getParent():获取父路径,返回String对象
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123.txt"); 4 System.out.println(file.getParent()); 5 } 6 }
输出结果:F:\
方法10:File getParentFile():获取父路径,返回File对象
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\123.txt"); 4 System.out.println(file.getParentFile()); 5 } 6 }
输出结果:F:\
方法11:boolean exists():判断File构造方法中封装路径是否存在
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("src"); 4 System.out.println(file.exists()); 5 } 6 }
输出结果:true
方法12:boolean isDirectory():判断File构造方法中封装的路径是不是文件夹
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("src"); 4 System.out.println(file.isDirectory()); 5 } 6 }
输出结果:true
方法13:boolean isFile():判断File构造方法中封装的路径是不是文件
同上
方法14:String[] list():获取File构造方法中封装路径中的文件和文件夹名(遍历一个目录),返回值只有文件名或文件夹名
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("E:\\ideaIU-2018.3.5.win"); 4 String[] fileNames = file.list(); 5 for(String fileName : fileNames) { 6 System.out.println(fileName); 7 } 8 } 9 }
输出结果:
bin build.txt help Install-Windows-zip.txt ipr.reg jre32 jre64 lib license plugins product-info.json redist
方法15:File[] listFiles():获取File构造方法中封装路径中的文件和文件夹名(遍历一个目录),返回的是目录或文件的全路径
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("E:\\ideaIU-2018.3.5.win"); 4 File[] files = file.listFiles(); 5 for(File f : files) { 6 System.out.println(f); 7 } 8 } 9 } 10
输出结果:
E:\ideaIU-2018.3.5.win\bin E:\ideaIU-2018.3.5.win\build.txt E:\ideaIU-2018.3.5.win\help E:\ideaIU-2018.3.5.win\Install-Windows-zip.txt E:\ideaIU-2018.3.5.win\ipr.reg E:\ideaIU-2018.3.5.win\jre32 E:\ideaIU-2018.3.5.win\jre64 E:\ideaIU-2018.3.5.win\lib E:\ideaIU-2018.3.5.win\license E:\ideaIU-2018.3.5.win\plugins E:\ideaIU-2018.3.5.win\product-info.json E:\ideaIU-2018.3.5.win\redist
方法16:File[] listRoots(): 列出可用的文件系统根
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("E:\\ideaIU-2018.3.5.win"); 4 File[] files = file.listRoots(); 5 for(File f : files) { 6 System.out.println(f); 7 } 8 } 9 }
输出结果:
C:\ D:\ E:\ F:\
方法17.文件过滤器:File[] listFiles(FileFilter filter):返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
遍历目录的时候,可以根据需要,只获取满足条件的文件
遍历目录方法File[] listFiles()的重载形式:File[] listFiles(FileFilter filter),listFiles(FileFilter filter)中传递的参数为一个接口的实现类对象,该实现类需由自己定义,重写抽象方法
在F盘下创建了一个Demo文件夹,该文件夹下包含两个文件,分别是123.txt和Demo.java。下面的程序将实现通过过滤器只获取后缀名为“.java”的文件
1.实现FileFilter接口,重写accept方法(返回值为boolean类型)
1 import java.io.File; 2 import java.io.FileFilter; 3 4 public class MyFilter implements FileFilter { 5 6 @Override 7 public boolean accept(File pathname) { 8 return pathname.getName().endsWith(".java"); 9 } 10 }
2.创建的接口FileFilter实现类对象
3.将该对象作为listFiles(FileFilter filter)方法的参数传入
1 public class FileConstructor { 2 public static void main(String[] args) throws IOException { 3 File file = new File("F:\\Demo"); 4 File[] files = file.listFiles(new MyFilter()); 5 for(File f : files) { 6 System.out.println(f); 7 } 8 } 9 }
输出结果:F:\Demo\Demo.java
listFiles()遍历目录的同时,获取到了文件的全路径名,调用过滤器的方法accept(),将获取到的路径传递给accept方法的参数pathname。在accept方法中进行判断,如果这个路径是java文件,返回true,否则,返回false。一旦方法返回了true,listFiles就会将路径保存到数组中。