File类的概述
java.io.File类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
File类部分构造方法
复制 | |
| |
| |
| private File(String child, File parent) { |
| ... |
| } |
| |
| |
| |
| |
| public File(String pathname) { |
| ... |
| } |
路径分隔符和默认名称分隔符
复制 | |
| |
| public static final char separatorChar = fs.getSeparator(); |
| |
| |
| public static final String separator = "" + separatorChar; |
| |
| |
| |
| |
| |
| |
| public static final char pathSeparatorChar = fs.getPathSeparator(); |
| |
| |
| public static final String pathSeparator = "" + pathSeparatorChar; |
依赖于系统的默认名称分隔符
复制 | import java.io.File; |
| |
| public class Demo01Separator { |
| public static void main(String[] args) { |
| |
| char separatorChar = File.separatorChar; |
| System.out.println("系统的默认名称分隔符字符是'" + separatorChar + "'"); |
| |
| |
| String separatorString = File.separator; |
| System.out.println("系统的默认名称分隔符字符是'" + separatorString + "'"); |
| } |
| } |
复制 | 控制台输出: |
| 系统的默认名称分隔符字符是'/' |
| 系统的默认名称分隔符字符是'/' |
依赖于系统的路径分隔符
复制 | import java.io.File; |
| |
| public class Demo01PathSeparator { |
| public static void main(String[] args) { |
| |
| char pathSeparatorChar = File.pathSeparatorChar; |
| System.out.println("依赖于系统的路径分隔符字符是'" + pathSeparatorChar + "'"); |
| |
| |
| String pathSeparatorString = File.pathSeparator; |
| System.out.println("依赖于系统的路径分隔符字符是'" + pathSeparatorString + "'"); |
| } |
| } |
复制 | 控制台输出: |
| 依赖于系统的路径分隔符字符是':' |
| 依赖于系统的路径分隔符字符是':' |
程序很多时候都是跨平台的,所以不要把路径写死,可以使用以上几个静态成员变量获取分隔符
File类的部分构造方法的使用
复制 | public class Demo02File { |
| public static void main(String[] args) { |
| |
| File file1 = new File("~/IdeaProjects/Study/src/view/study/demo27/Demo02File.java"); |
| System.out.println(file1); |
| |
| |
| File file2 = new File("~/IdeaProjects/Study/", "/src/view/study/demo27/Demo02File.java"); |
| System.out.println(file2); |
| } |
| } |
复制 | 控制台输出: |
| ~/IdeaProjects/Study/src/view/study/demo27/Demo02File.java |
| ~/IdeaProjects/Study/src/view/study/demo27/Demo02File.java |
File类获取功能的常用方法
复制 | public String getAbsolutePath() |
| |
| |
| public String getPath() |
| |
| |
| public String getName() |
| |
| |
| public long length() |
| |
这几个方法的使用
复制 | import java.io.File; |
| |
| public class Demo01FileMethod { |
| public static void main(String[] args) { |
| File file = new File("~/IdeaProjects/Study/src/view/study/demo27/Demo01FileMethod.java"); |
| |
| |
| String fileAbsolutePath = file.getAbsolutePath(); |
| |
| |
| String filePath = file.getPath(); |
| |
| |
| String fileName = file.getName(); |
| |
| |
| long fileLength = file.length(); |
| |
| System.out.println("绝对路径:" + fileAbsolutePath); |
| System.out.println("路径名字符串:" + filePath); |
| System.out.println("文件或目录的名称:" + fileName); |
| System.out.println("文件的长度:" + fileLength); |
| } |
| } |
复制 | 控制台输出: |
| 绝对路径:/Users/liyihua/IdeaProjects/Study/~/IdeaProjects/Study/src/view/study/demo27/Demo01FileMethod.java |
| 路径名字符串:~/IdeaProjects/Study/src/view/study/demo27/Demo01FileMethod.java |
| 文件或目录的名称:Demo01FileMethod.java |
| 文件的长度:0 |
文件路径不存在,返回文件的长度为0
File类判断功能的常用方法
复制 | public boolean exists() |
| |
| |
| public boolean isDirectory() |
| |
| |
| public boolean isFile() |
| |
这几个方法的使用
复制 | import java.io.File; |
| |
| public class Demo02FileMethod { |
| public static void main(String[] args) { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27"); |
| |
| boolean exiFile = file.exists(); |
| boolean isD = file.isDirectory(); |
| boolean isF = file.isFile(); |
| |
| System.out.println("文件或目录是否实际存在:" + exiFile); |
| System.out.println("是否为目录:" + isD); |
| System.out.println("是否为文件:" + isF); |
| } |
| } |
复制 | 控制台输出: |
| 文件或目录是否实际存在:true |
| 是否为目录:true |
| 是否为文件:false |
判断是否为文件或是否为目录的时候,路径必须存在,否则都返回false(计算机中只有文件和目录)
File类创建删除功能的常用方法
复制 | public boolean createNewFile() |
| |
| |
| public boolean delete() |
| |
| |
| public boolean mkdir() |
| |
| |
| public boolean mkdirs() |
| |
创建一个新的空文件
复制 | import java.io.File; |
| import java.io.IOException; |
| |
| public class Demo03FileMethod { |
| public static void main(String[] args) throws IOException { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27/test.txt"); |
| |
| file.createNewFile(); |
| } |
| } |
所在的目录下创建了一个文件——test.txt
删除一个文件或目录
复制 | import java.io.File; |
| import java.io.IOException; |
| |
| public class Demo04FileMethod { |
| public static void main(String[] args) throws IOException { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27/test.txt"); |
| |
| file.delete(); |
| } |
| } |
所在的目录下的一个文件(test.txt)被删除
创建一个目录
复制 | import java.io.File; |
| import java.io.IOException; |
| |
| public class Demo05FileMethod { |
| public static void main(String[] args) throws IOException { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27/test"); |
| |
| file.mkdir(); |
| } |
| } |
所在的目录下创建了一个目录——test
创建一个目录集(父子爷孙目录)
复制 | import java.io.File; |
| import java.io.IOException; |
| |
| public class Demo06FileMethod { |
| public static void main(String[] args) throws IOException { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27/t1/t2/t3"); |
| |
| file.mkdirs(); |
| } |
| } |
在目录“/Users/liyihua/IdeaProjects/Study/src/view/study/demo27”下创建了目录 /t1/t2/t3
File类目录的遍历功能
复制 | public String[] list() |
| |
| |
| public File[] listFiles() |
| |
这两个方法的使用:
复制 | import java.io.File; |
| |
| public class Demo07FileMethod { |
| public static void main(String[] args) { |
| File file = new File("/Users/liyihua/IdeaProjects/Study/src/view/study/demo27"); |
| |
| |
| String[] stringList = file.list(); |
| |
| File[] fileList = file.listFiles(); |
| |
| |
| for (String s : stringList) { |
| System.out.println(s); |
| } |
| System.out.println("\n\n"); |
| for (File f : fileList) { |
| System.out.println(f); |
| } |
| } |
| } |
复制 | 控制台输出: |
| Demo01PathSeparator.java |
| Demo01File.java |
| Demo01FileMethod.java |
| Demo06FileMethod.java |
| Demo07FileMethod.java |
| Demo02FileMethod.java |
| 了解File类 |
| Demo05FileMethod.java |
| Demo02File.java |
| Demo04FileMethod.java |
| Demo01Separator.java |
| Demo03FileMethod.java |
| |
| |
| |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo01PathSeparator.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo01File.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo01FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo06FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo07FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo02FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/了解File类 |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo05FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo02File.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo04FileMethod.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo01Separator.java |
| /Users/liyihua/IdeaProjects/Study/src/view/study/demo27/Demo03FileMethod.java |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)