Java files and Io--file classes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.