Java Notes Collation (10), file in Java

Source: Internet
Author: User

The file class provides a series of operational methods for reading and writing to directories and files

Create:
 Public classFileDemo01 { Public Static voidMain (string[] args) {Try {            //Create a fileFile File =NewFile ("FileName.txt"); if(File.createnewfile ()) {System.out.println ("Created successfully"); } Else{System.out.println ("Create Failed"); }            //Create a folderFile dir =NewFile ("DirName"); if(Dir.mkdir ()) {System.out.println ("Created successfully"); } Else{System.out.println ("Create not successful"); }            //Create a multi -level catalogFile dirs =NewFile ("Firstdir\\seconddir\\thirddir"); if(Dirs.mkdirs ()) {System.out.println ("Created successfully"); } Else{System.out.println ("Create not successful"); }        } Catch(IOException e) {e.printstacktrace (); }    }}

Delete
 Public classFileDemo01 { Public Static voidMain (string[] args) {//Deleting FilesFile FileName =NewFile ("D:\\java_workspace\\hellofile\\filename.txt"); if(Filename.delete ()) {System.out.println ("Delete Succeeded"); } Else{System.out.println ("Delete Failed"); }        //Delete directory, need this directory is emptyFile dir =NewFile ("D:\\java_workspace\\hellofile\\firstdir\\seconddir\\thirddir"); if(Dir.delete ()) {System.out.println ("Delete Succeeded"); } Else{System.out.println ("Delete Failed"); }        //Delete directory when exitingFile Dir2 =NewFile ("D:\\java_workspace\\hellofile\\firstdir\\seconddir");    Dir2.deleteonexit (); }}

Renaming
 Public classFileDemo01 { Public Static voidMain (string[] args)throwsIOException {File oldFile=NewFile ("C:\\name1.txt"); File NewFile=NewFile ("C:\\name2.txt"); //determines whether the file exists, if it does not exist, creates if it exists, prompts        if(!oldfile.exists ())            {Oldfile.createnewfile (); System.out.println ("Created successfully"); } Else{System.out.println ("File already exists"); }        //If the rename succeeds, the prompt succeeds or the prompt fails        if(Oldfile.renameto (newFile)) {System.out.println ("Rename succeeded"); } Else{System.out.println ("Renaming failed"); }    }}

In fact, the Renameto () method can also move folders, but there will be many problems under Windows, use caution, details click: http://xiaoych.iteye.com/blog/149328

Other methods
 Public classFileDemo01 { Public Static voidMain (string[] args)throwsIOException {File folder=NewFile ("D:/test/directory1/directory11"); File File=NewFile ("D:/test/directory1/directory11/file111.txt"); //System.out.println (Folder.mkdirs ()); //System.out.println (File.createnewfile ());        /*** Gets the name of the file object or the path returns a String * output: * Object Name: Directory11 * Object name: File111.txt *          */System.out.println ("Object Name:" +folder.getname ()); System.out.println ("Object Name:" +file.getname ()); /*** Gets the absolute path of the file object returns a String * output: * Absolute path: D:\test\Directory1\Directory11 * Absolute path: d:\ Test\directory1\directory11\file111.txt*/System.out.println ("Absolute path:" +Folder.getabsolutepath ()); System.out.println ("Absolute path:" +File.getabsolutepath ()); /*** Gets the path name of the file object returns the String * Output: * path name: D:\test\Directory1\Directory11 * path name: D:\test\Directory1\Directory11\File111.txt*/System.out.println ("Path name:" +Folder.getpath ()); System.out.println ("Path name:" +File.getpath ()); /*** Gets the parent directory of the file object returns a String * output: * Parent directory Name: D:\test\Directory1 * Parent directory Name: D:\test\Directory 1\directory11*/System.out.println ("Parent directory Name:" +folder.getparent ()); System.out.println ("Parent directory Name:" +file.getparent ()); /*** See if File object exists return Boolean * output: * True*/System.out.println (Folder.exists ()); /*** See if the file object is readable and writable returns a Boolean * return result: * readable * writable*/        if(File.canread ()) {System.out.println ("Readable"); }        if(File.canwrite ()) {System.out.println ("Writable"); }                /*** View the length of the file object return long * output result * 0kb*/System.out.println (file.length ()+ "KB"); /*** Return directory all sub-files and subdirectories array return file[] * Output Result: * Directory1 * Directory2 * Directory3 */File NewFile=NewFile ("D:/test"); File[] FileList=Newfile.listfiles ();  for(inti = 0; i < filelist.length; i++) {System.out.println (Filelist[i].getname ()); }    }}

Java Notes Collation (10), file in Java

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.