PackageCom.yyq;ImportJava.io.*;//File class/** used to encapsulate files or folders as objects * Convenient for manipulating file and folder property information * The files object can be passed as a parameter to a stream's constructor * Stream can only process data **//** Common methods of the file class: * 1. Create * Boolean createnewfile () * Create a file at the specified location, if the file already exists, do not create, return false * and output stream is not the same, output stream object --Create the file, the file exists will be overwritten. * mkdir file.mkdir Create a directory * mkdir file.mkdirs (Create a multilevel directory) you can either create a directory or create a file * 2. Remove * Delete Boolean type delete failed, return Back false Deleteonexit () void type tells the virtual machine to delete the specified file when exiting * 3. Judgment * CanExecute judgment can be executed * exists () determine if the file exists * * (//In judging the file Whether the object is a file or a directory, it must be judged whether it exists) * Isfile () Determine whether the file is a isdirectory determine if it is a directory * Ishidden () * Isabsolute determine if it is an absolute path * 4. Get information * getName () * GetPath () * getParent ()//Returns the parent directory under the absolute path, or null if a relative path is obtained * if there is a previous level of directory in the relative path, the directory is returned return results. * GetAbsolutePath () * getlastmodified (); * Renameto has the effect of moving*/ Public classFiledemotest { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stub//Consmethod (); //method_2 ();method_5 (); File dir=NewFile ("e:\\eclipse-Copy \\IOtest"); //Implementing filtering------anonymous internal classes//list is the filter file based on accept//File filtering------filtering Methodstring[] names = Dir.list (NewFilenameFilter () { Public BooleanAccept (File dir,string name) {/*System.out.println (dir+ "----" +name); if (Name.endswith (". txt")); return false;*/ returnName.endswith (". txt"); } }); System.out.println (names.length); for(String s:names) {System.out.println (s); } } //three ways to create a file object by creating a file Object Public Static voidConsmethod () {//encapsulates a.txt as a file object, which encapsulates existing and non-appearing files or folders into objectsFile f =NewFile ("A.txt"); //path unchanged, file can changeFile F1 =NewFile ("D:\\abc", "B.txt"); File D=NewFile ("D:" +file.separator+ "abc"); File f3=NewFile (D, "C.txt"); System.out.println (F1); System.out.println (d); System.out.println (F3); } Public Static voidMethod_1 ()throwsexception{File F=NewFile ("Asssddb.txt"); //tell the virtual machine to delete when exiting ... F.deleteonexit (); SOP ("Create:" +f.createnewfile ()); SOP ("Delete" +F.delete ()); } Public Static voidMethod_2 ()throwsexception{File F=NewFile ("A.txt"); SOP ("Create:" +f.canexecute () +f.exists ()); SOP (F.getpath ()); //You can list drive characters:File[]arr =file.listroots (); for(File R:arr) {System.out.println (R.getabsolutepath ()); } //The file object that invokes the list method must encapsulate the directory, which must also exist. //List of files can be listedFile File =NewFile ("C:\\users"); String []names=file.list (); for(String s:names) {System.out.println (s); } } Public Static voidsop (Object obj) {System.out.println (obj); } Public Static voidmethod_3 () {//Create folder Create directory//mkdir can only create one-level catalogs,File dir =NewFile ("Dd\\abc\\kk"); Dir.mkdirs (); System.out.println (Dir.getabsolutepath ()); System.out.println (Dir.getparentfile ()); } Public Static voidmethod_5 () {File dir=NewFile ("e:\\eclipse-Copy \\IOtest"); file[] Files=Dir.listfiles (); for(File f:files) {System.out.println (F.getname ()+"::"+f.length ()); } }}
PackageCom.yyq;ImportJava.io.*;//File class/** used to encapsulate files or folders as objects * Convenient for manipulating file and folder property information * The files object can be passed as a parameter to a stream's constructor * Stream can only process data **//** Common methods of the file class: * 1. Create * Boolean createnewfile () * Create a file at the specified location, if the file already exists, do not create, return false * and output stream is not the same, output stream object --Create the file, the file exists will be overwritten. * mkdir file.mkdir Create a directory * mkdir file.mkdirs (Create a multilevel directory) you can either create a directory or create a file * 2. Remove * Delete Boolean type delete failed, return Back false Deleteonexit () void type tells the virtual machine to delete the specified file when exiting * 3. Judgment * CanExecute judgment can be executed * exists () determine if the file exists * * (//In judging the file Whether the object is a file or a directory, it must be judged whether it exists) * Isfile () Determine whether the file is a isdirectory determine if it is a directory * Ishidden () * Isabsolute determine if it is an absolute path * 4. Get information * getName () * GetPath () * getParent ()//Returns the parent directory under the absolute path, or null if a relative path is obtained * if there is a previous level of directory in the relative path, the directory is returned return results. * GetAbsolutePath () * getlastmodified (); * Renameto has the effect of moving*/ Public classFiledemotest { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stub//Consmethod (); //method_2 ();method_5 (); File dir=NewFile ("e:\\eclipse-Copy \\IOtest"); //Implementing filtering------anonymous internal classes//list is the filter file based on accept//File filtering------filtering Methodstring[] names = Dir.list (NewFilenameFilter () { Public BooleanAccept (File dir,string name) {/*System.out.println (dir+ "----" +name); if (Name.endswith (". txt")); return false;*/ returnName.endswith (". txt"); } }); System.out.println (names.length); for(String s:names) {System.out.println (s); } } //three ways to create a file object by creating a file Object Public Static voidConsmethod () {//encapsulates a.txt as a file object, which encapsulates existing and non-appearing files or folders into objectsFile f =NewFile ("A.txt"); //path unchanged, file can changeFile F1 =NewFile ("D:\\abc", "B.txt"); File D=NewFile ("D:" +file.separator+ "abc"); File f3=NewFile (D, "C.txt"); System.out.println (F1); System.out.println (d); System.out.println (F3); } Public Static voidMethod_1 ()throwsexception{File F=NewFile ("Asssddb.txt"); //tell the virtual machine to delete when exiting ... F.deleteonexit (); SOP ("Create:" +f.createnewfile ()); SOP ("Delete" +F.delete ()); } Public Static voidMethod_2 ()throwsexception{File F=NewFile ("A.txt"); SOP ("Create:" +f.canexecute () +f.exists ()); SOP (F.getpath ()); //You can list drive characters:File[]arr =file.listroots (); for(File R:arr) {System.out.println (R.getabsolutepath ()); } //The file object that invokes the list method must encapsulate the directory, which must also exist. //List of files can be listedFile File =NewFile ("C:\\users"); String []names=file.list (); for(String s:names) {System.out.println (s); } } Public Static voidsop (Object obj) {System.out.println (obj); } Public Static voidmethod_3 () {//Create folder Create directory//mkdir can only create one-level catalogs,File dir =NewFile ("Dd\\abc\\kk"); Dir.mkdirs (); System.out.println (Dir.getabsolutepath ()); System.out.println (Dir.getparentfile ()); } Public Static voidmethod_5 () {File dir=NewFile ("e:\\eclipse-Copy \\IOtest"); file[] Files=Dir.listfiles (); for(File f:files) {System.out.println (F.getname ()+"::"+f.length ()); } }}
Io Stream file Listfiles method (important)