File对象在Java开发中经常需要碰到,其构造方法大家都比较熟悉了:
File(File dir,String name) 使用File对象路径和文件名进行创建句柄 |
File(String path) 只使用路径创建句柄 |
File(String dirPath,String name) 使用字符路径和文件名来创建句柄 |
File(URI uri) 使用URI路径来创建句柄 |
在使用构造函数之后,这个File对象就存在了,你可以使用File.exists()来对其进行判断,存在则返回true。
Java使用介乎于UNIX和Windows的路径方法,前斜线“/”可以正确解释,而如果要使用反斜线“\”则需要进行转义序列“\\”,才能被解释器正确解释。
很重要的一点就是,Java的File对象可以是文件也可以是文件夹。所以当你使用
File file=new File("/java");后
你返回的是文件夹,但是你在后面加一句
File fileNext=new File("/java/text.txt");
file使用isFile();的返回值就会变成了false。它变成了文件夹,也就是“目录”。
对于目录属性的File,我们可以使用list()来显示目录下的内容。
其他方法摘要,可以参考以下文档
扫描二维码关注公众号,回复:
1723259 查看本文章
方法摘要 | |
---|---|
boolean |
canRead() Indicates whether the current context is allowed to read from this file. |
boolean |
canWrite() Indicates whether the current context is allowed to write to this file. |
int |
compareTo(File another) Returns the relative sort ordering of the paths for this file and the file another . |
boolean |
createNewFile() Creates a new, empty file on the file system according to the path information stored in this file. |
static File |
createTempFile(String prefix,String suffix) Creates an empty temporary file using the given prefix and suffix as part of the file name. |
static File |
createTempFile(String prefix,String suffix, File directory) Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name. |
boolean |
delete() Deletes this file. |
void |
deleteOnExit() Schedules this file to be automatically deleted once the virtual machine terminates. |
boolean |
equals(Object obj) Compares obj to this file and returns true if they represent thesame object using a path specific comparison. |
boolean |
exists() Returns a boolean indicating whether this file can be found on the underlying file system. |
File |
getAbsoluteFile() Returns a new file constructed using the absolute path of this file. |
String |
getAbsolutePath() Returns the absolute path of this file. |
File |
getCanonicalFile() Returns a new file created using the canonical path of this file. |
String |
getCanonicalPath() Returns the absolute path of this file with all references resolved. |
String |
getName() Returns the name of the file or directory represented by this file. |
String |
getParent() Returns the pathname of the parent of this file. |
File |
getParentFile() Returns a new file made from the pathname of the parent of this file. |
String |
getPath() Returns the path of this file. |
int |
hashCode() Returns an integer hash code for the receiver. |
boolean |
isAbsolute() Indicates if this file's pathname is absolute. |
boolean |
isDirectory() Indicates if this file represents a directory on the underlying file system. |
boolean |
isFile() Indicates if this file represents a file on the underlying file system. |
boolean |
isHidden() Returns whether or not this file is a hidden file as defined by the operating system. |
long |
lastModified() Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight. |
long |
length() Returns the length of this file in bytes. |
String[] |
list() Returns an array of strings with the file names in the directory represented by this file. |
String[] |
list(FilenameFilter filter) Gets a list of the files in the directory represented by this file. |
File[] |
listFiles() Returns an array of files contained in the directory represented by this file. |
File[] |
listFiles(FileFilter filter) Gets a list of the files in the directory represented by this file. |
File[] |
listFiles(FilenameFilter filter) Gets a list of the files in the directory represented by this file. |
static File[] |
listRoots() Lists the file system roots. |
boolean |
mkdir() Creates the directory named by the trailing filename of this file. |
boolean |
mkdirs() Creates the directory named by the trailing filename of this file, including the complete directory path required to create this directory. |
boolean |
renameTo(File dest) Renames this file to the name represented by the dest file. |
boolean |
setLastModified(long time) Sets the time this file was last modified, measured in milliseconds since January 1st, 1970, midnight. |
boolean |
setReadOnly() Marks this file or directory to be read-only as defined by the operating system. |
String |
toString() Returns a string containing a concise, human-readable description of this file. |
URI |
toURI() Returns a Uniform Resource Identifier for this file. |
URL |
toURL() Returns a Uniform Resource Locator for this file. |