One. FILE CLASS
1. The Class File object describes the information of the files and directories under the specified path. With File objects, you can view files and directory information under a path, and operate the files and directories under the path.
2. Application of Class File
2. View (query) File information under a path.
2.1.1 Constructor of Class File
Class file has two types of constructor, a class of parameters, used to generate an object of a file system under the path pointed to by the parameter; the other class has two numbers, the first parameter representation path, the second A parameter representation subdirectory that generates file information that describes a subdirectory under the path pointed to by the first parameter, and the subdirectory name is specified by the second parameter. Such as:
File path = new file ("c: //"); generates an object describing file information under C: /
File path = new file ("c: //", "test"); generates an object that describes the file information of the C: / Test subdirectory.
2.1.2 List () and List (filenamefilter filter) functions
1) The List () function returns the file information described in the File object in the form of a String array. Returns the file name and directory name of all files and directories under the path.
2) List (filenamefilter filter) provides query features, which can query file names and directory names that meet specific conditions.
2.1.3 Query for file or directory with the FineNameFilter interface
2.1.3.1. This interface contains only a function: Boolean Accept (File Dir, String Name), the second parameter represents the name of a file or directory, the first parameter represents the name of the directory where the file or directory is located. When Returns True, the file meets the query condition, to be placed in the result; otherwise, the file is not satisfied.
2.1.3.2. Working principle
When calling a file object's List (FileNameFilter Filter), the Accept () method in each file or directory in the File object is called. When the Accept () function is called for a file or directory, it is transmitted to the Accept () function as the file object that describes the directory it.
2.1.4 A simple example
Import java.io.file;
Import java.io.filenamefilter;
Class dirlist {
Public void printfile (String Dir, string filter) {
File path = new file (DIR);
String [] FileList;
IF (Filter.length () == 0) {
System.out.println ("/ NALL FILE:");
FileList = path.list (); // acquire all file information
}
Else {
System.out.println ("/ Nall File Including" Filter;
// obtain file information that meets the query conditions
FileList = path.list (New Dirfilter (Filter);
}
For (int i = 0; i System.out.println (fileList [i]); } Public void printfile (String Dir) { Printfile (dir, "); } Class Dirfilter Implements FileNameFilter { String AFN; // Store query conditions Dirfilter (String Afn) {this.afn = af; / / Make query conditions, return true Public Boolean Accept (File Dir, String Name) { Return Name.indexof (AFN)! = - 1; } } Public class testio { Public static void main (String [] args) { Dirlist dirlist = new dirlist (); // Display file information under F: / TEST Dirlist.printfile ("f: // Test"); // Display f: / test name contains ".rar" file information Dirlist.printfile ("f: // Test", ".rar"); // Display f: / test name contains "R" file information Dirlist.printfile ("f: // Test", "R"); } } This is just a simple example, just improve the accept () function, you can also make a Java version of the DIR command. 2.2 Of course, we can also create, delete, and rename an operation with file or directory. Import java.io.file; Import java.io.filenamefilter; Import java.util.date; Import java.text.SIMPLEDATEFORMAT; Class Dirfilter Implements FileNameFilter { String AFN; Dirfilter (String Afn) {this.afn = af; Public Boolean Accept (File Dir, String Name) { Return Name.indexof (AFN)! = - 1; } } Class Optfile { Public Static String GetabsolutePath (file f) { Return f.getabsolutePath (); } Public stat String getpath (file f) { Return f.getParent (); } Public static string getname (file f) { Return f.getname (); } Public Static Long getLength (file f) { Return f.Length (); } Public static string getparent (file f) { Return f.getParent (); } Public static string getlastmodified (file f, string format) { SimpleDateFormat SDF = New SimpleDateFormat ("YYYY-MM-DD"); IF (Format.Equals ("YMDH")) SDF = New SimpleDateFormat ("YYYY-MM-DD HH"); Else IF (Format.Equals ("YMDHM")) SDF = New SimpleDateFormat ("YYYY-MM-DD HH: MM"); Return SDF.Format ((New Date (f.lastmodified ()))))))) } Public static string getlastmodified (file f) {return getlastmodified (f, "ymd"); } Public static boolean canread (file f) { Return f.canread (); } Public static boolean canwrite (file f) { Return f.canwrite (); } Public static boolean isfile (file f) { Return f.isfile (); } Public static boolean isdir (file f) { Return f.Indirectory (); } Public Static Boolean Rename (File OldName) {{NEWNAME Return OldName.Renameto (NewName); } Public Static Boolean Delete (file f) { Return f.delete (); } Public static boolean mkdir (file f) { Return f.mkdirs (); } / ** * Query files from the specified path * / Public static file [] findfile (string filepath, string filename) { FILE F = New File (FilePath); File [] result = f.listfiles (New Dirfilter (FileName)); Return Result; } Public Static Void PrintFileAtt (file f) { System.out.println ( "Absolute Path:" GetabsolutePath (f) "/ n can read:" canread (f) "/ N CAN Write:" Canwrite (f) "/ n path:" getPath (f) "/ n name:" GetName (f) "/ n parent:" getParent (f) "/ N Length:" getLength (f) "/ n lastmodified:" getLastModified (f)); IF (isfile (f)) System.out.Println ("It's a file"); ELSE IF (isdir (f)) System.out.Println ("It's a directory"); } } Public class testio { Public static void main (String [] args) { File f1 = new file ("f: // nepalon // thinkinginjava // Test"); File f2 = new file ("f: // nepalon // thinkinginjava // Test // AA"); Optfile.PrintFileAtt (f1); Optfile.PrintFileAtt (F2); / / When the file or directory is renamed, and when it is updated to a different next time, // will first cut the file or complete the directory to the new directory, rename it again. File nf2 = new file ("F: // Nepalon // Thinkinginjava // Use log4j.files"); if (Optfile.Rename (F2, NF2)) { System.out.println ("After Rename F2:"); Optfile.PrintFileAtt (NF2); } Else System.out.println ("Rename Nf2 Failly"); // When deleting an operation, you can only delete the file; if you delete a directory, you will return false File df = new file ("f: //nepalon//thinkinginjava//test//1001.doc"); IF (Optfile.delete (DF)) System.out.println ("Delete DF SuccessFully); Else System.out.println ("delete df failly"); System.out.println ("Find File with Name /" 1001 / "); Optfile.FindFile ("f: // nepalon // thinkinginjava // TEST", "1001"); File mf = new file ("f: // nepalon // thinkinginjava // test /// 1001"); IF (Optfile.mkdir (MF)) System.out.Println ("Create Directory MF SuccessFully); Else System.out.println ("CREATE DIRECTORY MF FAILLN); } }