Java手册
java.io
类 File
java.lang.Object
java.io.File
所有已实现的接口:
Serializable, Comparable< File>
public class Fileextends Objectimplements Serializable, Comparable<File>
文件和目录路径名的抽象表示形式。
用户界面和操作系统使用与系统相关的_路径名字符串_ 来命名文件和目录。此类呈现分层路径名的一个抽象的、与系统无关的视图。抽象路径名 有两个组件:
- 一个可选的与系统有关的_前缀_ 字符串,比如盘符,
"/"
表示 UNIX 中的根目录,"\\\\"
表示 Microsoft Windows UNC 路径名。 - 零个或更多字符串_名称_ 的序列。
抽象路径名中的第一个名称是目录名,对于 Microsoft Windows UNC 路径名则是主机名。抽象路径名中第一个名称之后的每个名称表示一个目录;最后一个名称既可以表示目录,也可以表示文件。空 抽象路径名没有前缀和名称序列。
路径名字符串与抽象路径名之间的转换与系统有关。将抽象路径名转换为路径名字符串时,每个名称与下一个名称之间用一个默认_分隔符_ 隔开。默认名称分隔符由系统属性 file.separator
定义,可通过此类的公共静态字段 [`separator`](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23separator)
和 [`separatorChar`](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23separatorChar)
使其可用。将路径名字符串转换为抽象路径名时,可以使用默认名称分隔符或者底层系统支持的任何其他名称分隔符来分隔其中的名称。
无论是抽象路径名还是路径名字符串,都可以是_绝对_ 路径名或_相对_ 路径名。绝对路径名是完整的路径名,不需要任何其他信息就可以定位它所表示的文件。相反,相对路径名必须使用取自其他路径名的信息进行解释。默认情况下,java.io
包中的类总是根据当前用户目录来解析相对路径名。此目录由系统属性 user.dir
指定,通常是 Java 虚拟机的调用目录。
调用此类的 getParent()
方法可以获取抽象路径名的_父_ 路径名,它由路径名前缀以及路径名名称序列中的每个名称(最后一个除外)组成。对于任何具有绝对抽象路径名的 File 对象,如果其绝对抽象路径名以某个目录的绝对路径名开头,那么该目录的绝对路径名是该 File 对象的祖先。例如,抽象路径名 "/usr" 表示的目录是路径名 "/usr/local/bin" 所表示目录的一个祖先。
在处理 UNIX 平台的根目录,以及 Microsoft Windows 平台的盘符、根目录和 UNC 路径名时,将用到前缀这一概念。如下所示:
- 对于 UNIX 平台,绝对路径名的前缀始终是
"/"
。相对路径名没有前缀。表示根目录的绝对路径名的前缀为"/"
且名称序列为空。 - 对于 Microsoft Windows 平台,包含盘符的路径名前缀由驱动器号和一个
":"
组成。如果路径名是绝对路径名,还可能后跟"\\"
。UNC 路径名的前缀是"\\\\"
;主机名和共享名是名称序列中的前两个名称。没有指定驱动器的相对路径名没有前缀。
此类的实例可能表示(也可能不表示)实际文件系统对象,如文件或目录。如果它表示这种对象,那么该对象驻留在一个_分区_ 中。分区是文件系统特定于操作系统的存储分区。一个存储设备(例如,物理磁盘驱动器、闪存、CD-ROM)可以包含多个分区。对象(如果有)将驻留在此路径名(绝对形式)某个祖先指定的分区上。
文件系统可以实现对实际文件系统对象上的某些操作(比如,读、写、执行)进行限制。这些限制统称为_访问权限_。文件系统可以对一个对象设置多个访问权限。例如,一个设置可能适用于对象的_所有者_,另一个设置则可能适用于所有其他用户。对象上的访问权限可能导致此类的某些方法执行失败。
File
类的实例是不可变的;也就是说,一旦创建,File
对象表示的抽象路径名将永不改变。
从以下版本开始:
JDK1.0
另请参见:
字段摘要
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[pathSeparator](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23pathSeparator)**
与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
static char
**[pathSeparatorChar](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23pathSeparatorChar)**
与系统有关的路径分隔符。
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[separator](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23separator)**
与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
static char
**[separatorChar](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23separatorChar)**
与系统有关的默认名称分隔符。
构造方法摘要
**[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23File%28java.io.File%2C+java.lang.String%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html) parent, [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) child)
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File
实例。
**[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23File%28java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) pathname)
通过将给定路径名字符串转换为抽象路径名来创建一个新 File
实例。
**[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23File%28java.lang.String%2C+java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) parent, [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) child)
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File
实例。
**[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23File%28java.net.URI%29)**([URI](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fnet%2FURI.html) uri)
通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。
方法摘要
boolean
**[canExecute](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23canExecute%28%29)**()
测试应用程序是否可以执行此抽象路径名表示的文件。
boolean
**[canRead](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23canRead%28%29)**()
测试应用程序是否可以读取此抽象路径名表示的文件。
boolean
**[canWrite](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23canWrite%28%29)**()
测试应用程序是否可以修改此抽象路径名表示的文件。
int
**[compareTo](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23compareTo%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html) pathname)
按字母顺序比较两个抽象路径名。
boolean
**[createNewFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23createNewFile%28%29)**()
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
static [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)
**[createTempFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23createTempFile%28java.lang.String%2C+java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) prefix, [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) suffix)
在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。
static [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)
**[createTempFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23createTempFile%28java.lang.String%2C+java.lang.String%2C+java.io.File%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) prefix, [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html) suffix, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html) directory)
在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。
boolean
**[delete](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23delete%28%29)**()
删除此抽象路径名表示的文件或目录。
void
**[deleteOnExit](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23deleteOnExit%28%29)**()
在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。
boolean
**[equals](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23equals%28java.lang.Object%29)**([Object](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html) obj)
测试此抽象路径名与给定对象是否相等。
boolean
**[exists](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23exists%28%29)**()
测试此抽象路径名表示的文件或目录是否存在。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)
**[getAbsoluteFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getAbsoluteFile%28%29)**()
返回此抽象路径名的绝对路径名形式。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[getAbsolutePath](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getAbsolutePath%28%29)**()
返回此抽象路径名的绝对路径名字符串。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)
**[getCanonicalFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getCanonicalFile%28%29)**()
返回此抽象路径名的规范形式。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[getCanonicalPath](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getCanonicalPath%28%29)**()
返回此抽象路径名的规范路径名字符串。
long
**[getFreeSpace](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getFreeSpace%28%29)**()
返回此抽象路径名指定的分区中未分配的字节数。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[getName](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getName%28%29)**()
返回由此抽象路径名表示的文件或目录的名称。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[getParent](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getParent%28%29)**()
返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null
。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)
**[getParentFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getParentFile%28%29)**()
返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null
。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[getPath](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getPath%28%29)**()
将此抽象路径名转换为一个路径名字符串。
long
**[getTotalSpace](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getTotalSpace%28%29)**()
返回此抽象路径名指定的分区大小。
long
**[getUsableSpace](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23getUsableSpace%28%29)**()
返回此抽象路径名指定的分区上可用于此虚拟机的字节数。
int
**[hashCode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23hashCode%28%29)**()
计算此抽象路径名的哈希码。
boolean
**[isAbsolute](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23isAbsolute%28%29)**()
测试此抽象路径名是否为绝对路径名。
boolean
**[isDirectory](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23isDirectory%28%29)**()
测试此抽象路径名表示的文件是否是一个目录。
boolean
**[isFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23isFile%28%29)**()
测试此抽象路径名表示的文件是否是一个标准文件。
boolean
**[isHidden](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23isHidden%28%29)**()
测试此抽象路径名指定的文件是否是一个隐藏文件。
long
**[lastModified](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23lastModified%28%29)**()
返回此抽象路径名表示的文件最后一次被修改的时间。
long
**[length](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23length%28%29)**()
返回由此抽象路径名表示的文件的长度。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)[]
**[list](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23list%28%29)**()
返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)[]
**[list](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23list%28java.io.FilenameFilter%29)**([FilenameFilter](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFilenameFilter.html) filter)
返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)[]
**[listFiles](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23listFiles%28%29)**()
返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)[]
**[listFiles](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23listFiles%28java.io.FileFilter%29)**([FileFilter](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFileFilter.html) filter)
返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)[]
**[listFiles](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23listFiles%28java.io.FilenameFilter%29)**([FilenameFilter](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFilenameFilter.html) filter)
返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
static [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html)[]
**[listRoots](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23listRoots%28%29)**()
列出可用的文件系统根。
boolean
**[mkdir](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23mkdir%28%29)**()
创建此抽象路径名指定的目录。
boolean
**[mkdirs](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23mkdirs%28%29)**()
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
boolean
**[renameTo](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23renameTo%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html) dest)
重新命名此抽象路径名表示的文件。
boolean
**[setExecutable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setExecutable%28boolean%29)**(boolean executable)
设置此抽象路径名所有者执行权限的一个便捷方法。
boolean
**[setExecutable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setExecutable%28boolean%2C+boolean%29)**(boolean executable, boolean ownerOnly)
设置此抽象路径名的所有者或所有用户的执行权限。
boolean
**[setLastModified](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setLastModified%28long%29)**(long time)
设置此抽象路径名指定的文件或目录的最后一次修改时间。
boolean
**[setReadable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setReadable%28boolean%29)**(boolean readable)
设置此抽象路径名所有者读权限的一个便捷方法。
boolean
**[setReadable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setReadable%28boolean%2C+boolean%29)**(boolean readable, boolean ownerOnly)
设置此抽象路径名的所有者或所有用户的读权限。
boolean
**[setReadOnly](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setReadOnly%28%29)**()
标记此抽象路径名指定的文件或目录,从而只能对其进行读操作。
boolean
**[setWritable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setWritable%28boolean%29)**(boolean writable)
设置此抽象路径名所有者写权限的一个便捷方法。
boolean
**[setWritable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23setWritable%28boolean%2C+boolean%29)**(boolean writable, boolean ownerOnly)
设置此抽象路径名的所有者或所有用户的写权限。
[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FString.html)
**[toString](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23toString%28%29)**()
返回此抽象路径名的路径名字符串。
[URI](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fnet%2FURI.html)
**[toURI](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23toURI%28%29)**()
构造一个表示此抽象路径名的 file: URI。
[URL](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fnet%2FURL.html)
**[toURL](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Fio%2FFile.html%23toURL%28%29)**()
已过时。 此方法不会自动转义 URL 中的非法字符。建议新的代码使用以下方式将抽象路径名转换为 URL:首先通过 toURI
方法将其转换为 URI,然后通过 URI.toURL
方法将 URI 装换为 URL。
从类 java.lang.Object 继承的方法
[clone](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23clone%28%29), [finalize](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23finalize%28%29), [getClass](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23getClass%28%29), [notify](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23notify%28%29), [notifyAll](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23notifyAll%28%29), [wait](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23wait%28%29), [wait](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23wait%28long%29), [wait](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.runoob.com%2Fmanual%2Fjdk1.6%2Fjava%2Flang%2FObject.html%23wait%28long%2C+int%29)
实际应用
创建一个文件
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException{
//将传入的路径标记为了一个file对象
File file = new File("E:\\test");
//创建file对象指定的文件
boolean b2 = file.createNewFile();
System.out.println(b2);
}
}
运行结果:
true
E 盘里面出现了新的文件夹
注意:
1、创建文件的时候需要调用方法:createNewFile()
2、File file = new File("E:\\test");
先把路径传给 file 对象,然后**createNewFile()**这个方法开始根据传过来的路径检测是否在此路径下有对应的文件,
如果有,则创建失败,返回flase,如果没有,则创建指定的文件,并返回true。
3、只能创建文件不能创建目录
删除一个文件
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException{
//将传入的路径标记为了一个file对象
File file = new File("E:\\test");
//删除文件file对象对应的文件
boolean b1 = file.delete();
System.out.println(b1);
}
}
运行结果:
true
此时,file 对象指向的文件被删除。
注意:
1、删除文件或者目录的过程是不可逆的
2、删除目录时,要求目录必须是空目录
创建一层目录
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException{
//将传入的路径标记为了一个file对象
File file = new File("E:\\test");
//创建一层目录
boolean b = file.mkdir();
System.out.println(b);
}
}
运行结果:
注意:
1、当且仅当这个目录不存在的时候才会创建
2、只能创建一层目录
创建多层目录
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) throws IOException{
//将传入的路径标记为了一个file对象
File file = new File("E:\\test\\cjj\\lnn");
//创建多层目录
boolean b = file.mkdirs();
System.out.println(b);
}
}
运行结果: