First, the file class
The file class is somewhat misleading, and it may be easy to assume that it refers to a document, which is not, and it can represent the name of a particular file and the name of a group of files in a directory. In short, the file class is an abstract representation of the path names of files or directories that can help us deal with file directory problems.
Note: The file class can only manipulate properties of files, the contents of which are not operational and require flow to operate.
Second, the use of the file class
1 Public classFiledemo {2 3 Public Static voidMain (string[] args) {4 5File file2 =NewFile ("E:\\test\\a.txt");//in particular, it is important to note that such path forms are valid under Windows6 7File File3 =NewFile ("E:\\test");//building a file from a path8 9File file4 =NewFile ("E:", "Test");//building a file from the parent and child classpath namesTen OneFile file5 =NewFile (File3, "a.txt");//building a file from the parent class file and child classpath names A - System.out.println (file2); - System.out.println (file3); the System.out.println (file4); - System.out.println (file5); -System.out.println (File.separator);//delimiter for consecutive multiple path strings -System.out.println (File.pathseparator);//That separates the directories in the same path string. +file[] Files =file.listroots (); - for(File file:files) { + System.out.println (file); A } at - } - -}
Result output:
E:\test\a.txte:\teste:\teste:\test\a.txt; C:d:e:f:\
Iii. Common methods of File class
①, creating methods
1.boolean CreateNewFile () to eradicate the given path creation file, does not exist return true existence returns false
2.boolean mkdir () Create a directory if the previous level directory does not exist, it will create a failure
3.boolean mkdirs () creates a multi-level directory that is automatically created if the previous directory does not exist
②, Delete method
1.boolean Delete () deletes a file or directory, and if it represents a directory, the directory must be empty to delete
2.boolean deleteonexit () file is deleted after use is complete
③, judging method
1.boolean CanExecute () Determine if the file is executable
2.boolean canRead () Determine if the file is readable
3.boolean canWrite () Determine if the file is writable
4.boolean exists () determine if a file or directory exists
5.boolean isdirectory () Determine if this path is a directory
6.boolean Isfile () to determine whether a file
7.boolean Ishidden () determines if the file is hidden
8.boolean Isabsolute () determines whether an absolute path file does not exist or can be judged
④, obtaining methods
1.String GetName () Gets the file or directory name represented by this path
2.String GetPath () converts this path name to a pathname string
3.String GetAbsolutePath () returns the absolute form of this abstract path name
4.String getParent ()//If no parent directory returns null
5.long lastmodified ()//Gets the last modified time
6.long () returns the length of the file represented by this abstract path name.
7.boolean Renameto (file f) Renames files represented by this abstract path name.
8.file[] Liseroots ()//Get Machine drive letter
9.string[] List () returns an array of strings that name the files and directories in the directory represented by this abstract path name.
10.string[] List (FilenameFilter filter) returns a string array that names the files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
The specific use is as follows (pick a few commonly used):
1 Public classFileDemo2 {2 3 Public Static voidMain (string[] args) {4 5File File =NewFile ("E:\\test.txt");6 Try {7 //Create a file8System.out.println (File.exists ());//determine if a file exists9System.out.println (File.createnewfile ());//create file, empty file, size 0TenSystem.out.println (File.exists ());//determine if a file exists One //get file-related properties ASystem.out.println (File.length ());//File Size -System.out.println (NewDate (File.lastmodified ()));//file Last modified time -System.out.println (File.getname ());//Get file name theSystem.out.println (File.getpath ());//Get file path -System.out.println (File.getabsolutepath ());//Get absolute path -System.out.println (File.getparent ());//Get Parent Path -System.out.println (File.getparentfile ());//gets the parent file + - //determine the relevant characteristics of the document +System.out.println (File.canexecute ());//can execute ASystem.out.println (File.canread ());//can read and write atSystem.out.println (File.canwrite ());//can read and write -System.out.println (File.isfile ());//whether it is a file -System.out.println (File.ishidden ());//is a hidden file -}Catch(Exception e) { - e.printstacktrace (); - } in - to } + -}
Result output:
false true true021:47:32 CST 2018test.txte:\test.txte:\test.txte:e:truetrue true true false
Java IO Learning--file class