Java的File对象

来源:互联网 发布:js获取frame 编辑:程序博客网 时间:2024/05/22 03:40

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()来显示目录下的内容。

其他方法摘要,可以参考以下文档

方法摘要 booleancanRead()
          Indicates whether the current context is allowed to read from this file. booleancanWrite()
          Indicates whether the current context is allowed to write to this file. intcompareTo(File another)
          Returns the relative sort ordering of the paths for this file and the fileanother. booleancreateNewFile()
          Creates a new, empty file on the file system according to the path information stored in this file.static FilecreateTempFile(String prefix,String suffix)
          Creates an empty temporary file using the given prefix and suffix as part of the file name.static FilecreateTempFile(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. booleandelete()
          Deletes this file. voiddeleteOnExit()
          Schedules this file to be automatically deleted once the virtual machine terminates. booleanequals(Object obj)
          Compares obj to this file and returns true if they represent thesame object using a path specific comparison. booleanexists()
          Returns a boolean indicating whether this file can be found on the underlying file system. FilegetAbsoluteFile()
          Returns a new file constructed using the absolute path of this file. StringgetAbsolutePath()
          Returns the absolute path of this file. FilegetCanonicalFile()
          Returns a new file created using the canonical path of this file. StringgetCanonicalPath()
          Returns the absolute path of this file with all references resolved. StringgetName()
          Returns the name of the file or directory represented by this file. StringgetParent()
          Returns the pathname of the parent of this file. FilegetParentFile()
          Returns a new file made from the pathname of the parent of this file. StringgetPath()
          Returns the path of this file. inthashCode()
          Returns an integer hash code for the receiver. booleanisAbsolute()
          Indicates if this file's pathname is absolute. booleanisDirectory()
          Indicates if this file represents a directory on the underlying file system. booleanisFile()
          Indicates if this file represents a file on the underlying file system. booleanisHidden()
          Returns whether or not this file is a hidden file as defined by the operating system. longlastModified()
          Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight. longlength()
          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. booleanmkdir()
          Creates the directory named by the trailing filename of this file. booleanmkdirs()
          Creates the directory named by the trailing filename of this file, including the complete directory path required to create this directory. booleanrenameTo(File dest)
          Renames this file to the name represented by the dest file. booleansetLastModified(long time)
          Sets the time this file was last modified, measured in milliseconds since January 1st, 1970, midnight. booleansetReadOnly()
          Marks this file or directory to be read-only as defined by the operating system. StringtoString()
          Returns a string containing a concise, human-readable description of this file. URItoURI()
          Returns a Uniform Resource Identifier for this file. URLtoURL()
          Returns a Uniform Resource Locator for this file.

原创粉丝点击