Packagecom. File;ImportJava.io.File;ImportJava.io.FilenameFilter;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classFileDemo1 { Public Static voidMain (string[] args) {}/** We want to implement IO operations, we have to know the representation of the file on the hard disk * and Java provides a filename class for us to use * * File: Abstract representation of File and directory Pathname * Construction Method: * File (String pathname) * File (string parent,string child) * File (file parent,string child)*/ Public voidtest01 () {//encapsulate the D:\\demo\\a.txt as a file objectFile file=NewFile ("E:\\demo\\a.txt"); File file2=NewFile ("E:\\demo", "A.txt"); File File3=NewFile ("E:\\demo"); File file4=NewFile (File3, "A.txt"); } /**public Boolean createnewfile (): When and only if there is no creation *public Boolean mkdir () if and only if no folder exists *public Boolean mkdirs () Create a folder that will automatically be created if the parent folder does not exist * * riding a white horse is not necessarily the prince, may be the monitor * Note: Whether you want to create a file or a folder, you know best, method do not tune the wrong*/ Public voidtest02 ()throwsioexception{//I'm going to have to create a folder in the E-disk directory. DemoFile file=NewFile ("E:\\demo"); System.out.println ("MkDir:" +File.mkdir ()); //I want to create a file in the E-drive directory demo A.txtFile file2=NewFile ("E:\\demo\\a.txt"); System.out.println ("CreateNewFile:" +file2.createnewfile ()); //I want to create a file under the E-drive directory test B.txt//Exception in thread "main" java.io.IOException: The system cannot find the path specified. //Note: To create content under a directory, the directory must first exist//file File3=new file ("E:\\test\\b.txt");//System.out.println ("CreateNewFile:" +file3.createnewfile ()); //I want to create a file AAA directory under the E-drive directory test//file File4=new file ("E:\\test\\aaa");//System.out.println ("mkdir:" +file4.mkdir ()); //file File5=new file ("E:\\test");//file File6=new file ("E:\\test\\aaa");//System.out.println ("mkdir:" +file5.mkdir ());//System.out.println ("mkdir:" +file6.mkdir ());File File7=NewFile ("e:\\aaa\\bbb\\ccc\\ddd"); System.out.println ("Mkdirs:" +file7.mkdirs ()); File File8=NewFile ("E:\\test\\ddd\\x.txt"); System.out.println ("Mkdirs:" +file8.mkdirs ()); } /** Delete function: public boolean Delete () * * NOTE: * A: If you create a file or folder forgot to write the letter, the path, then the default under the project path * B:ja Delete in VA does not go to Recycle Bin * C: If you want to delete a folder, note that the folder cannot contain folders or text*/ Public voidtest03 ()throwsioexception{//file File=new file ("E:\\a.txt");//System.out.println ("CreateNewFile:" +file.createnewfile ()); //I accidentally wrote it this way .File file=NewFile ("A.txt"); System.out.println ("CreateNewFile:" +file.createnewfile ()); File file2=NewFile ("AAA\\BBB\\CCC"); System.out.println ("Mkdirs:" +file2.mkdirs ()); File File3=NewFile ("A.txt"); System.out.println ("Delete:" +File3.delete ()); File file4=NewFile ("AAA\\BBB\\CCC"); System.out.println ("Delete:" +File4.delete ()); //file File5=new file ("AAA");//System.out.println ("Delete:" +file5.delete ());//to delete a folder, you must first delete its child elementsFile File6=NewFile ("AAA\\BBB"); File File7=NewFile ("AAA"); System.out.println ("Delete:" +File6.delete ()); System.out.println ("Delete:" +File7.delete ()); } /** Rename function: Public boolean renameto (File dest) * If the path name is the same, change the name * If the pathname is different, it is renamed and clipped * * Path begins with drive letter: absolute path: c:\\a.txt * Path not easy drive letter start: relative path a.txt*/ Public voidtest04 () {//file File=new file ("Brigitte. jpg");//if (!file.exists ()) {//file.createnewfile ();// } //requirements, I want to modify the name of this file for oriental unbeaten. jpg//file Newfile=new file ("Oriental unbeaten. jpg");//System.out.println ("Renameto:" +file.renameto (NewFile));File file2=NewFile ("Oriental undefeated. jpg"); File NewFile2=NewFile ("e:\\ Brigitte. jpg"); System.out.println ("Renameto:" +File2.renameto (newFile2)); } /** Judgment Function: * Public boolean isdirectory () * Public boolean isfile () * Public Boolean exists ( ) * Public boolean canRead () * Public boolean canWrite () * Public boolean Ishidden () *
*/ Public voidtest05 () {File file=NewFile ("A.txt"); System.out.println ("Isdirectory:" +file.isdirectory ()); System.out.println ("Isfile:" +file.isfile ()); System.out.println ("Exists:" +file.exists ()); System.out.println ("CanRead:" +File.canread ()); System.out.println ("CanWrite:" +file.canwrite ()); System.out.println ("Ishidden:" +File.ishidden ()); } /** Get Function: * public string GetAbsolutePath () get Absolute path * public string GetPath () get Relative path * public string get Name * Public long length () Gets the length, number of bytes * Public long lastmodified () Gets the last modified time, millisecond value **/ Public voidtest06 () {//file File=new file ("demo");//file.mkdirs ();//file File1=new file ("Demo\\text.txt");//file1.createnewfile ();File File=NewFile ("Demo\\text.txt"); System.out.println ("GetAbsolutePath:" +File.getabsolutepath ()); System.out.println ("GetPath:" +File.getpath ()); System.out.println ("GetName:" +file.getname ()); System.out.println ("Length:" +file.length ()); System.out.println ("LastModified:" +file.lastmodified ()); //SimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println (Sdf.format (NewDate (File.lastmodified ())); } /** Get function: * Public string[] List (): Gets the name array of all files or folders under the specified directory * public file[] Listfiles () gets all the files in the specified directory or The file array of the record*/ Public voidtest07 () {File file=NewFile ("e:\\"); String[] Strarray=file.list (); for(String s:strarray) {System.out.println (s); } System.out.println ("----------------"); File[] Filearray=File.listfiles (); for(File f:filearray) {System.out.println (F.getname ()); } } /** Using the file name filter, similar to the comparator used in TreeSet * * Determine if there is a file with a suffix. jpg in the E-drive directory, and if so, the output of this file name * * Idea: * First get all, And then the condition is satisfied and the output is satisfied, then the output can be * * To achieve this effect, it is necessary to learn an interface: FilenameFilter file name filter * Public string[] Li St (filenamefilter Filter) * Public file[] Listfiles (filenamefilter filter)*/ Public voidtest08 () {File file=NewFile ("e:\\"); //get a string array of all files or folders in this directoryString[] Strarray=file.list (NewFilenameFilter () {@Override Public BooleanAccept (File dir, String name) {//System.out.println (dir+ "--------" +name);//file File=new file (dir,name);//System.out.println (file);//boolean flag=file.isfile ();//boolean Flag2=name.endswith (". jpg");//return flag && Flag2; return NewFile (Dir,name). Isfile () && name.endswith (". jpg"); } }); for(String s:strarray) {System.out.println (s); } } //Small Test /** Determine if there is a file with a . jpg suffix in the e-drive directory, and if so, output this file name **/ Public voidtest09 () {File file=NewFile ("e:\\"); file[] Files=File.listfiles (); for(File f:files) {if(F.isfile ()) {if(F.getname (). EndsWith (". jpg") {System.out.println (F.getname ()); } } } } }
Java.io.File