Java Learning -011-Creating file instances and source code

Source: Internet
Author: User

The source code of this article is mainly for the application of Java to create files. If there is any deficiency, please ask the great God to correct me, I appreciate it!

The source code for creating the file is as follows:

1     /**2 * @function file operation: Create file. If the file does not exist, the file is created directly; if the file exists and is not occupied, delete the file and recreate the file3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java createFile, 2015-2-2 21:03:24 Exp $6      * 7      * @paramFileName : File name8      * @paramDelete : Delete the original file9      * Ten      * @returnBoolean returns True if the file was successfully created; One      *  A      * @throwsException -      */ -      Public BooleanCreateFile (String filename,BooleanDelete) { the         BooleanFlag =false; -          -         Try { -File File =NewFile (filename); +              -             if(!file.exists ()) {//file does not exist, create file directly + file.createnewfile (); A                  This. message = filename + "created successfully. "; at                  This. Logger.info ( This. message); -                  -Flag =true; -}Else if(File.exists () && file.renameto (file)) {//file exists and is not occupied -                 if(delete) {//delete files and recreate files - File.delete (); in                      This. message = filename + "Delete succeeded. "; -                      This. Logger.info ( This. message); to                      + file.createnewfile (); -                      This. message = filename + "created successfully. "; the                      This. Logger.info ( This. message); *                      $Flag =true;Panax Notoginseng}Else { -Flag =true; the                 } +}Else{//file already exists and is occupied, exit Operation A                  This. Message = "{" + filename + "} file is occupied, unable to write operation! "; the                  This. Logger.warn ( This. message); +             } -}Catch(Exception e) { $              This. Message =e.getmessage (); $              This. Logger.error ( This. message); -         } -          the         returnFlag; -     }Wuyi      the     /** - * @function file operation: Create file. If the file does not exist, the file is created directly; if the file exists and is not occupied, delete the file and recreate the file Wu      *  -      * @authorAARON.FFP About      * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java createFile, 2015-2-2 21:03:24 Exp $ $      *  -      * @paramFileName : File name -      *  -      * @returnBoolean returns True if the file was successfully created; A      *  +      * @throwsException the      */ -      Public BooleancreateFile (String filename) { $         BooleanFlag =false; the          the         Try { theFile File =NewFile (filename); the              -             if(!file.exists ()) {//file does not exist, create file directly in file.createnewfile (); the                  This. message = filename + "created successfully. "; the                  This. Logger.info ( This. message); About                  theFlag =true; the}Else if(File.exists () && file.renameto (file)) {//file exists and is not occupied, delete file and recreate file the File.delete (); +                  This. message = filename + "Delete succeeded. "; -                  This. Logger.info ( This. message); the                 Bayi file.createnewfile (); the                  This. message = filename + "created successfully. "; the                  This. Logger.info ( This. message); -                  -Flag =true; the}Else{//file already exists and is occupied, exit Operation the                  This. Message = "{" + filename + "} file is occupied, unable to write operation! "; the                  This. Logger.warn ( This. message); the             } -}Catch(Exception e) { the              This. Message =e.getmessage (); the              This. Logger.error ( This. message); the         }94          the         returnFlag; the}
Create file source code

At this point, Java Learning -011-to create a file instance and the source code is successfully completed, I hope this article can give beginners Java you a reference.

Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^

Java Learning -011-Creating file instances and source code

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.