Overview of the File class:
The file class is an object that encapsulates files and folders in the file system, and can manipulate files and folders through the idea of an object. The file class holds various meta-data information for files or directories, including file name, file length, last modified time, whether it is readable, gets the path name of the current file, determines whether the specified file exists, obtains a list of files in the current directory, and creates and deletes files and directories.
How to construct the file class:
public File (String pathname) // Span style= "color: #008000;" The absolute path to the file public file (Uri uri) // Span style= "color: #008000;" > The URI address of the file public file (string parent, String child) // Specify the parent file absolute Path, the sub-file absolute path public File (file parent, String child) // Specify parent file, sub-file relative path // These are the private constructors in the file class and cannot be called outside Span style= "color: #0000ff;" >private File (String child, File parent) File (String pathname, int prefixlength)
In general, we only use the first construction method.
Other functional methods of the file class:
To create a file:
New File (path); Boolean B = File.createnewfile ();
To create a directory folder:
New File (path); Boolean B = File.mkdir ();
To create a multilevel directory folder:
New File (path); Boolean B = File.mkdirs ();
Judging method:
BooleanFile.exists ()//whether the file existsBooleanFile.isfile ()//whether it is a fileBooleanFile.isdirectory ()//whether it is a directoryBooleanFile.ishidden ()//whether to hide (you can set a file to hide on Windows)BooleanFile.isabsolute ()//is an absolute pathBooleanFile.canread ()//whether it is readableBooleanFile.canwrite ()//whether it can be writtenBooleanFile.canexecute ()//whether it can be executed
Get file information:
String File.getname ()//get the name of the file, just the name, no pathString file.getparent ()//gets the absolute path of the parent directory, and the return value is a string. If the file has a parent directory, then the absolute path of the parent directory is returned (for example: ' E:\cat '), if the file itself is at the root of the disk, then the path of the disk is returned (for example: ' E:\ '). File file.getparentfile ()//gets the parent file that the return value is a file object. LongTime = File.lastmodified ();//returns the time the file was last modifiedDate dt =NewDate (time);BooleanRenameto (File file)//file namingLongFile.length ()//returns the size of the file, in bytesBooleanFile.delete ()//Deleting Filesstring[] File.list ()//gets the name of all the files under the directory. If ' file ' is a file, the return value is ' null ' and will be empty when used, but if ' file ' is a directory, then the name of all files in this directory is returned, only the name, not the path, and if ' file ' is an empty directory, returns an array of length 0, as shown above , the ' list () ' method is only valid for ' file ' as a directory, and the method is meaningless when ' file ' is a file. file[] File.listfiles ()//gets all the files in the directory. If ' file ' is a file, the return value is ' null ' and will be empty when used, but if ' file ' is a directory, then all files in this directory are returned, and if ' file ' is an empty directory, it returns an array of length 0, as shown in the above results, ' listfiles ( ) ' method, which is only valid for ' file ' as a directory, when ' file ' is a file, the method is meaningless.
Java Basic IO Flow summary two: Basic application of File class