📜  Java.io.File类

📅  最后修改于: 2020-11-15 02:41:41             🧑  作者: Mango


介绍

Java.io.File类是文件和目录路径名的抽象表示。以下是关于File的要点-

  • 实例可以表示也可以不表示实际的文件系统对象,例如文件或目录。如果它确实表示这样的对象,则该对象驻留在分区中。分区是文件系统存储的特定于操作系统的部分。

  • 文件系统可能会对实际文件系统对象的某些操作实施限制,例如读取,写入和执行。这些限制统称为访问权限。

  • File类的实例是不可变的。也就是说,一旦创建,由File对象表示的抽象路径名将永远不会改变。

类声明

以下是Java.io.File类的声明-

public class File
   extends Object
      implements Serializable, Comparable

领域

以下是Java.io.File类的字段-

  • 静态字符串pathSeparator-这是系统相关的路径分隔字符,为方便起见以字符串表示。

  • 静态char pathSeparatorChar-这是系统相关的路径分隔字符。

  • 静态字符串分隔符-这是系统相关的默认名称分隔字符,为方便起见,表示为字符串。

  • 静态字符分隔符-这是系统相关的默认名称分隔字符。

类的构造函数

Sr.No. Constructor & Description
1

File(File parent, String child)

This method creates a new File instance from a parent abstract pathname and a child pathname string.

2

File(String pathname)

This method creates a new File instance by converting the given pathname string into an abstract pathname.

3

File(String parent, String child)

This method creates a new File instance from a parent pathname string and a child pathname string.

4

File(URI uri)

This method Creates a new File instance by converting the given file : URI into an abstract pathname.

类方法

Sr.No. Method & Description
1 boolean canExecute()

This method tests whether the application can execute the file denoted by this abstract pathname.

2 boolean canRead()

This method tests whether the application can read the file denoted by this abstract pathname.

3 boolean canWrite()

This method tests whether the application can modify the file denoted by this abstract pathname.

4 int compareTo(File pathname)

This method compares two abstract pathnames lexicographically.

5 boolean createNewFile()

This method atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

6 static File createTempFile(String prefix, String suffix)

This method creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

7 static File createTempFile(String prefix, String suffix, File directory)

This method Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

8 boolean delete()

This method deletes the file or directory denoted by this abstract pathname.

9 void deleteOnExit()

This method requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

10 boolean equals(Object obj)

This method tests this abstract pathname for equality with the given object.

11 boolean exists()

This method tests whether the file or directory denoted by this abstract pathname exists.

12 File getAbsoluteFile()

This method returns the absolute form of this abstract pathname.

13 String getAbsolutePath()

This method returns the absolute pathname string of this abstract pathname.

14 File getCanonicalFile()

This method returns the canonical form of this abstract pathname.

15 String getCanonicalPath()

This method returns the canonical pathname string of this abstract pathname.

16 long getFreeSpace()

This method returns the number of unallocated bytes in the partition named by this abstract path name.

17 String getName()

This method returns the name of the file or directory denoted by this abstract pathname.

18 String getParent()

This method returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.

19 File getParentFile()

This method returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory.

20 String getPath()

This method converts this abstract pathname into a pathname string.

21 long getTotalSpace()

This method returns the size of the partition named by this abstract pathname.

22 long getUsableSpace()

This method returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.

23 int hashCode()

This method computes a hash code for this abstract pathname.

24 boolean isAbsolute()

This method tests whether this abstract pathname is absolute.

25 boolean isDirectory()

This method tests whether the file denoted by this abstract pathname is a directory.

26 boolean isFile()

This method tests whether the file denoted by this abstract pathname is a normal file.

27 boolean isHidden()

This method tests whether the file named by this abstract pathname is a hidden file.

28 long lastModified()

This method returns the time that the file denoted by this abstract pathname was last modified.

29 long length()

This method returns the length of the file denoted by this abstract pathname.

30 String[] list()

This method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

31 String[] list(FilenameFilter filter)

This method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

32 File[] listFiles()

This method returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

33 File[] listFiles(FileFilter filter)

This method returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

34 File[] listFiles(FilenameFilter filter)

This method returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

35 static File[] listRoots()

This method lists the available filesystem roots.

36 boolean mkdir()

This method creates the directory named by this abstract pathname.

37 boolean mkdirs()

This method creates the directory named by this abstract pathname, including any necessary but non existent parent directories.

38 boolean renameTo(File dest)

This method renames the file denoted by this abstract pathname.

39 boolean setExecutable(boolean executable)

This is a convenience method to set the owner’s execute permission for this abstract pathname.

40 boolean setExecutable(boolean executable, boolean ownerOnly)

This method Sets the owner’s or everybody’s execute permission for this abstract pathname.

41 boolean setLastModified(long time)

This method sets the last-modified time of the file or directory named by this abstract pathname.

42 boolean setReadable(boolean readable)

This is a convenience method to set the owner’s read permission for this abstract pathname.

43 boolean setReadable(boolean readable, boolean ownerOnly)

This method sets the owner’s or everybody’s read permission for this abstract pathname.

44 boolean setReadOnly()

This method marks the file or directory named by this abstract pathname so that only read operations are allowed.

45 boolean setWritable(boolean writable)

This is a convenience method to set the owner’s write permission for this abstract pathname.

46 boolean setWritable(boolean writable, boolean ownerOnly)

This method sets the owner’s or everybody’s write permission for this abstract pathname.

47 String toString()

This method returns the pathname string of this abstract pathname.

48 URI toURI()

This method constructs a file : URI that represents this abstract pathname.

方法继承

此类从以下类继承方法-

  • Java对象