一、File类
(一)概述
- A:什么是File类
- 其实File就是 文件夹或者文件路径(包括存在或者不存在)
- B:File的路径
- 相对路径:当前项目的相对路径
- 绝对路径:Windows下的盘符路径
- C:File的构造方法
-
- public File(String pathname) {} //根据文件路径名获得该文件的对象
-
- public File(String parent,String child) {} //根据上一级文件目录下的 子文件名 得到子文件的对象
-
- public File(File parent,String child) {} // 根据上一级文件对象 和文件名 获得该文件的对象
-
- F:获得文件对象
- File f = new File();//根据需求选择具体的构造器
- E:文件路径符
- Windows连接符:File.separator \
- Windows文件路径分隔符:File.pathSeparator ;
(二)文件方法
-
A:创建、删除、判断、修改 文件或者文件夹
-
创建:
- 1.file.createNewFile(); //创建新文件
- 2.file.mkdir();//创建文件夹
- 3.file.mkdirs();
- //创建已有上级文件夹的文件夹
- //创建上级文件夹以及其子文件夹
- 4.创建临时文件
//当前临时文件的文件全名
// 前缀 后缀
File createTempFile = File.createTempFile(“Mytest”, “.bak”);
//前缀 后缀 指定文件夹路径
File createTempFile = File.createTempFile(“prefix”, “.abc”, new File(“D://”));
//在程序结束再删除
createTempFile.deleteOnExit(); - 5.循环创建文件夹及子文件夹
-
删除:
- 1.file.delete();
- //删除文件(包含带有权限的文件,需要赋予管理员权限)
- //删除文件夹(空目录文件夹)
- 1.file.delete();
-
判断:
- public boolean canExecute():判断文件或者文件夹是否有可执行权限
- 文件夹的可执行权限:find命令查找
- public boolean isDirectory():判断是否是目录
- public boolean isFile():判断是否是文件
- public boolean exists():判断是否存在
- public boolean canRead():判断是否可读,默认都是可读,setReadable无效
- public boolean canWrite():判断是否可写,可以设置
- public boolean isHidden():判断是否隐藏,windows中先设置隐藏
- public boolean canExecute():判断文件或者文件夹是否有可执行权限
-
修改:
- public boolean renameTo(File dest):把重命名为指定的文件路径或剪切
- 注意:
- 路径名相同,修改文件名
- 路径名不同,就是改名并剪切
- 注意:
- public boolean renameTo(File dest):把重命名为指定的文件路径或剪切
-
-
B:获取File路径、文件内容字节长度、修改时间
-
路径:File file = new File(“src/com/briup/ch18/Test1.java”);
-
1.得到文件路径 file.getPath();
- src\com\briup\ch18\Test1.java
-
2.获得标准路径 file.getCanonicalPath();
- E:\spring-tool-suite-4-4.3.1.RELEASE-e4.12.0-win32.win32.x86_64\my-workspace\JD1908_corejava\src\com\briup\ch18\Test1.java
-
3.对于java项目中文件的读取
-
1、使用System 或是 系统的Properties对象
//a、当前文件所在的项目名
new File("").getCanonicalPath();//b、系统配置文件获取项目路径 String property = System.getProperty("user.dir"); System.out.println(property); //结果 E:\spring-tool-suite-4-4.3.1.RELEASE-e4.12.0-win32.win32.x86_64\my-workspace\JD1908_corejava //c、系统的属性,由此其实还是绕到使用 user.dir 属性来取得当前项目的真实路径 Properties properties = System.getProperties(); Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String name = (String) propertyNames.nextElement(); System.err.println(name); } //结果 user.dir 2、第二种方式:使用当前类的类加载器进行获取 ClassLoader 获取Class字节码实例,三种方式:(比如我的类叫Demo) ① Demo.class ② Class.forName("类的全称") ③ 利用Demo的实例对象,调用对象的getClass()方法获取该对象的Class实例 2.1回顾了如何获取Class字节码实例之后,然后再来回顾一下,如何获取ClassLoader对象 ① Demo.class.getClassLoader() ② Class.forName("类的全称").getClassLoader() ③ 假设demo为Demo的实例化对象 demo.getClass().getClassLoader() ④ 通过Thread对象的getContextClassLoader() 方法来获取 Thread.currentThread().getContextClassLoader() 记住哦,这里的getSystemResource方法获取的是URL对象,需要调用getPath()方法获取路径 1、当只是获取 log4j.properties 文件输入流的时候可以通过以下两种方式 ① 依然是使用 ClassLoader, 其中有两个方法,两者一个是静态一个非静态 ClassLoader.getSystemResourceAsStream("config/log4j.properties"); Thread.currentThread().getContextClassLoader().getResourceAsStream("config/log4j.properties"); ② 先通过File文件包装之后,然后新建一个输入流 File file01 = new File("config/log4j.properties"); System.out.println(file01.getAbsolutePath()); File file02 = new File(properties.getProperty("user.dir") + "/bin/config/log4j.properties"); System.out.println(file02.getAbsolutePath()); //ClassLoader.getSystemResource获取的是URL对象 File file03 = new File(ClassLoader.getSystemResource("config/log4j.properties").getPath()); System.out.println(file03.getAbsolutePath()); 其中创建file03 的方式不建议采纳,因为getSystemResource方法如果没获取到文件,则得到的 URL对象为null,此时再调用getPath()就会报错 ```
-
4.获得项目运行bin文件夹的路径new File(FilePath.class.getResource("/").getPath()).getCanonicalPath();
- E:\spring-tool-suite-4-4.3.1.RELEASE-e4.12.0-win32.win32.x86_64\my-workspace\JD1908_corejava\bin
-
5.获取当前文件的执行路径(.class文件所在路径)new File(FilePath.class.getResource("").getPath().getCanonicalPath();
* E:\spring-tool-suite-4-4.3.1.RELEASE-e4.12.0-win32.win32.x86_64\my-workspace\JD1908_corejava\bin\com\briup\ch18
-
6.当前文件所在的E盘符的根目录
new File("/").getCanonicalPath();- E:\
-
7.绝对路径 new File("…/a.txt").getAbsolutePath();
-
注意:8.getResource(); //返回 本地资源/网络资源 的URL资源路径
System.err.println(FilePath.class.getResource("/")); 输出结果: file:/E:/spring-tool-suite-4-4.3.1.RELEASE-e4.12.0-win32.win32.x86_64/my-workspace/JD1908_corejava/bin/
-
-
获取文件内容字节长度
- 1.获取文件长度
//存在文件 返回文件内容字节长度
//不存在文件 返回0
new File(“a.txt”).length();
- 1.获取文件长度
-
获得父文件夹路径及对象:
//1.获得父路径文件
String parent = file.getParent();
//2.获得父路径文件对象
File parentFile = file.getParentFile();
//3.父路下径创建文件 同级目录
File file2 = new File(parent,“a.txt”);File file2 = new File(parentFile,“a.txt”);
-
获得时间戳及时间对象(util包下)
//1.返回文件最后修改时间
//返回long类型数字 时间戳毫秒
new File(“src/com/briup/ch18/FileOperate.java”).lastModified();//2.修改文件时间Date
new Date(file3.lastModified()).toLocaleString();//3.获取当前日期对象
Date date = new Date();
//获得当前时间戳
long time = date.getTime();//一小时3600000
-
-
注意:通过获取当前时间和文件修改时间的差值 和 一小时的时间戳 进行判断,就可以找出一小时内修改的所有文件及文夹进行文件操作
-
遍历list集合文件名
* //1.list获取文件夹及文件的所有名字String[] list = file.list();
* //2.list获取文件夹及文件的所有文件类型对象
File[] listFiles = file3.listFiles();
* 递归调用 遍历该文件夹下所有的文件夹及文件 ```java public static void show() { File[] listFiles = file3.listFiles(); for( File file : listFiles) { if(isFile()) { syso(file.getName()); }else { show(file); } } } ```
-
文件过滤器(重写accept方法选择合适文件)
* ```java
FilenameFilter也是一个函数式接口
FilenameFilter filter = new FilenameFilter() {//dir 路径 //name 文件名 @Override public boolean accept(File dir, String name) { boolean endsWith = name.endsWith(".java"); return endsWith; } }; FilenameFilter filter1 = (dir,name) -> name.endsWith(".java"); String[] list3 = file2.list(filter1); for (String string : list3) { System.err.println("过滤器....."+string); } }
-
额外常用方法补充:
- listRoots();列出可以使用的磁盘(系统根路径);
- setReadOnly();设置文件为只读
- setWritable();设置能否写入
- setReadable();设置能否读取
- setExecutable();设置能否执行
- setLastModified(1365880248445L)设置最后修改时间
- Path toPath();返回java.nio.file.Path对象
- getFreeSpace();返回当前分区的可用空间
- getTotalSpace();返回当前分区总共大小