File class
File is a java. io package that represents platform-independent files and directories, that is, if you want to manipulate files and directories in a program through the file class, it is worth noting that both files and directories are manipulated using file. file can create, delete, and rename files and directories. file cannot access the content itself, if you need to access the file content itself, you need to use input, output stream
Accessing Files and directories
The file class can use a path string of files to create A file instance, which can be either an absolute path or a relative path, by default the system always interprets the relative path based on the user's working path, which has system properties "User.dir", usually the path where the Java Virtual Runtime is running
Once you have created a file object, you can manipulate files and directories through this object's methods
Methods for accessing file names
- String getName (): Returns the file or pathname represented by this object (if it is a path, returns the last level of the subpath name)
- String GetPath (): Returns the path name defined by the File Object
- File Getabsolutefile (): Returns the file object corresponding to the absolute path for this file
- String GetAbsolutePath (): Returns the absolute path name for this file object
- String getParent (): Returns the parent directory name of the corresponding directory for this file object (the last level of subdirectories)
- Boolean Renameto (File NewName): Renames the file or directory corresponding to this files object , and returns true successfully;
Methods related to file detection
- Boolean exits (): Determines whether the directory or file corresponding to the files object exists
- boolean canwrite (): Determines whether the directory or file that corresponds to the files object exists
- Boolean CanRead (): Determines whether the directory or file corresponding to the files object is readable
- boolean isfile (): Determine this file is file instead of directory
- bolean isdirectory (): Judging file is the object a directory instead of a file
- boolean isabsolute () : Determines whether the file or directory corresponding to the files object is an absolute path. This method eliminates the differences of different platforms, can directly judge file unix linux bsd /, then this is the absolute path, in window
If the start is a drive letter on the system
Get information about a regular file
- Long LastModified (): Returns the last modified time of the file
- Long Length (): Returns the length of the file contents
Methods related to file operations
- Boolean createnewfile (): The method creates a file that is specified by a file object when it does not exist, creating a successful return True
- Boolean delete (): Deletes the file or directory corresponding to the files object
- Static file Createtempfile (String prefix,string subffix): Creates a temporary empty file in the default temporary file directory, using the given prefix, the system-generated random number and the given suffix as the file name. This is a static method that can be called between the File classes. Prefix must be three bytes long, subffix can be null, in which case the default suffix is . tmp
- Static file Createtempfile (String prefix,string buffix,file directory): Creates a temporary empty file in the specified directory, using the given prefix, the system-generated random number and the given suffix as the file name. This is a static method that is called directly through the File class grams
- void Deleteonexit (): Registers a delete hook to specify that directories and files corresponding to file are deleted when the Java virtual machine exits
- Boolean mkdir (): An attempt was made to create a directory corresponding to a file object, or False if the creation returned successfully , note that the file object corresponds to a path, Rather than a file
- String[] List (): Lists the corresponding sub-files and path names of the file object, returning a String Array
- File[] Listfiles ((): Lists all the file pairs that correspond to all the sub-files and paths, returns a file array
- Static Listroots (): Lists all root paths of the system. This is a static method that can be called by the File class
And look back on the 18th day of Java