Concept:
File class: Represents an abstract representation of the path names of files and directories.
The file class can be used to create, delete, rename, get paths, create time, and so on, and is the only action class related to the file itself.
Cases:
Public classFiledemo {/** * * @paramTarget Directory folder *@paramext Extension*/ //find files under a folder Public Static voidFindFile (File target,string ext) {if(target!=NULL){ if(Target.isdirectory ()) {file[] files=Target.listfiles (); if(files!=NULL){ for(File f:files) {findfile (f,ext); } } }Else{String path=Target.getabsolutepath (); if(Path.endswith (EXT)) {System.out.println (path); } } } } Public Static voidMain (string[] args) {//TODO Auto-generated method stubs//Create a file object that points to a document or directory (files and directories do not necessarily exist)File file=NewFile ("C:" +file.separator+ "A.txt"); System.out.println ("File exists:" +file.exists ()); //Create a file if(!file.exists ()) { Try{file.createnewfile (); }Catch(IOException e) {e.printstacktrace (); }} System.out.println ("File absolute path:" +File.getabsolutepath ()); LongLastmodified=file.lastmodified ();//Last modification TimeDateFormat df=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Lasttime=df.format (NewDate (lastmodified)); System.out.println ("Last modified:" +lasttime); System.out.println ("Length of File:" +file.length ()); System.out.println ("Is directory" +file.isdirectory ()); File file2=NewFile ("C:\\codingke_test"); if(!file2.exists ()) {File2.mkdir ();//Create a directory } //System.out.println (); //File2.delete ();//Deleting FilesString[] Names=file2.list (); for(String name:names) {System.out.println (name); } file[] Files=File2.listfiles (); for(File f:files) {System.out.println (F.getpath ()+"--"+f.length ()); } findfile (NewFile ("C:\\codingke_test"), ". txt"); } }
Java files and Io--file classes