1. File
1.1, additions and deletions awarded
1 Packagelearn2;2 /*Common methods of the file class:3 * 1, create4 * Boolean createnewfile ();//creates if, if the file already exists in the specified location, does not create, returns false;5 * The output stream will overwrite6 * Boolean mkdir ();//Create Folder7 * mkdirs ();//create multi-level folders8 * 9 * 2, deleteTen * Boolean Delete ();//delete failed, return false One * void Deleteonexit ();//delete on program exit A * 3. Judgment - * Boolean exists ();//File exists - * Isfile (); the * Isdirectory (); - * Ishidden (); - * Isabsolute (); test whether this abstract pathname is an absolute path, even if it does not exist, it can be judged - * + * 4. Access to Information - * GetName (); + * GetPath (); A * GetParent (); at * - * GetAbsolutePath (); - * long lastmodify (); - * long Length (); - * - * */ in ImportJava.io.File; - Importjava.io.IOException; to + Public classFiledemo { - Public Static voidMain (string[] args)throwsioexception{ the method_4 (); * } $ Public Static voidMethod_5 ()throwsIOExceptionPanax Notoginseng { -File F1 =NewFile ("C:\\test.java"); theFile F2 =NewFile ("C:\\hahaha.java"); +SOP ("rename" +F1.renameto (F2)); A the } + Public Static voidMethod_4 ()throwsIOException - { $File f =NewFile ("C:\\abc\\a.txt"); $SOP ("Path:" +f.getpath ());//What path do you encapsulate, return what -SOP ("Abspath:" +f.getabsolutepath ());//no matter what the package is. Returns the absolute path to the directory to which it belongs -SOP ("Parent:" +f.getparent ());//the method returns the parent directory of the file in the absolute path, and returns an empty if the relative path is obtained the //If there is a previous level of directory in the relative path. The directory is the result of the return - }Wuyi Public Static voidMethod_3 ()throwsIOException the { -File f =NewFile ("file.txt"); Wu f.createnewfile (); - //Remember, when judging whether a file object is a file or a directory, you must first determine whether the file object encapsulates the existence of the content About //Judging by exists $SOP ("dir:" +f.isdirectory ()); -SOP ("File:" +f.isfile ()); - SOP (F.isabsolute ()); - } A Public Static voidMethod_2 ()throwsIOException + { theFile f =NewFile ("file.txt"); - //SOP ("Execute:" +f.canexecute ()); $ //Create a folder theFile dir =NewFile ("ABC"); the //only one level directory can be created the //SOP ("mkdir:" +dir.mkdir ()); theSOP ("mkdir:" +dir.mkdirs ()); - } in Public Static voidMethod_1 ()throwsIOException the { theFile f=NewFile ("file.txt"); AboutF.deleteonexit ();//Delete When the program exits the //SOP ("Create:" +f.createnewfile ()); theSOP ("Delete:" +F.delete ()); the } + - the Bayi Public Static voidConsmethod () the { the //encapsulates a.txt as a file object, which encapsulates existing and non-appearing files or folders into objects -File f1=NewFile ("A.txt"); - // theFile f2=NewFile ("C:\\abc", "B.txt"); the theFile d =NewFile ("C:\\abc"); theFile f3=NewFile (D, "C.txt"); - theSOP ("F1:" +F1); theSOP ("F2:" +F2); theSOP ("F3:" +f3);94 theFile F4 =NewFile ("C:" +file.separator+ "abc\\"); the the }98 Public Static voidsop (Object obj) About { - System.out.println (obj);101 }102}
1.2. List of files
The object that invokes the list method must be a directory of real existence
File[] Listfiles (filenamefilter filter) An array of file names filtered by the file name filter
1 Packagelearn2;2 3 ImportJava.io.File;4 ImportJava.io.FilenameFilter;5 6 Public classFileDemo2 {7 Public Static voidMain (string[] args) {8File dir =NewFile ("c:\\");9file[] Files =dir.listfiles ();Ten for(File f:files) One { ASystem.out.println (F.getname () + "::" +f.length ()); - } - } the Public Static voidlistdemo_2 () - { -File dir =NewFile ("c:\\"); - //Inner class +string[] arr = dir.list (NewFilenameFilter () - { + Public BooleanAccept (File dir, String name) A { at //System.out.println ("dir:" +dir); - //System.out.println ("Name:" +name); - //if (Name.endswith (". bmp")) - //return true; - //Else - //return false; in returnName.endswith (". bmp"); - } to }); +System.out.println ("Leng:" +arr.length); - for(String Name:arr) the { * System.out.println (name); $ }Panax Notoginseng } - Public Static voidListdemo () the { +File f =NewFile ("c:\\"); Astring[] names = F.list ();//contains a hidden file, the call list must encapsulate a real-world directory the for(String name:names) + { - System.out.println (name); $ } $ } - Private Static voidListrootdemo () { - //TODO auto-generated Method Stub theFile[] Files=file.listroots (); - for(File f:files)Wuyi { the System.out.println (f); - } Wu - } About}
Run the result as an array of filenames under the corresponding directory
1.3.
Dark Horse programmer--java Study 13 (BI 20--21 summary)--io