File类
2023.3.15
File
类是java.io包中的一个实体类,是关于io流操作的一个类,官方的描述是说
/**
* An abstract representation of file and directory pathnames.
*
* User interfaces and operating systems use system-dependent pathname
* strings to name files and directories. This class presents an
* abstract, system-independent view of hierarchical pathnames. An
* abstract pathname has two components:
*
*
* An optional system-dependent prefix string,
* such as a disk-drive specifier,"/" for the UNIX root
* directory, or "\\\\" for a Microsoft Windows UNC pathname, and
*
* A sequence of zero or more string names.
* /
翻译后
文件和目录路径名的抽象表示。
用户界面和操作系统使用依赖于系统的路径名字符串来命名文件和目录。
这个类提供了一个抽象的、独立于系统的层次路径名视图。抽象路径名有两个组件:
可选的系统相关前缀字符串,例如磁盘驱动器说明符,UNIX根目录为“/”,
Microsoft Windows UNC路径名为“\\\\”,以及
零个或多个字符串名称的序列。
常用方法
getName()
获取文件名getParent()
获取父级目录getPath()
获取最后一级目录或者文件名getAbsolutePath()
获取当前路径length()
获取文件大小exists()
判断文件是否存在isFile()
判断是否为文件isDirectory()
判断是否为文件夹createNewFile()
创建一个文件mkdir()
创建单级目录mkdirs()
创建联级目录list()
遍历该目录下所有目录与文件