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 file deletion source code is as follows:
1 /**2 * @function file operation: Delete file. If the file exists and is not occupied, the file is deleted, and if the file exists and is occupied, the file occupancy information is recorded, and if the file does not exist, the corresponding log information3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java deletefile, 2015-2-2 21:03:41 Exp $6 * 7 * @paramFileName: File full path8 * 9 * @returnBoolean Delete successfully returns TRUETen */ One Public BooleanDeleteFile (String filename) { A BooleanFlag =false; - - Try { theFile f =NewFile (filename); - - if(F.exists () &&F.renameto (f)) { - F.delete (); + ThisThe. Message = "{" + filename + "} file was successfully deleted. "; - This. Logger.info ( This. message); + AFlag =true; at}Else if(F.exists () &&!F.renameto (f)) { - This. Message = "{" + filename + "} file is in use and cannot be deleted! "; - This. Logger.warn ( This. message); -}Else{ - ThisThe. Message = "{" + filename + "} file does not exist. "; - This. Logger.warn ( This. message); in } -}Catch(Exception e) { to This. Message =e.getmessage (); + This. Logger.error ( This. message); - } the * returnFlag; $}
file Delete Source code
The test file deletion source code is as follows:
1 /**2 * Test: Delete file-fileutils.deletefile (String)3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java DeleteFile, March 9, 2015 PM 11:53:38 Exp $6 */7 @Test8 Public voidDeleteFile () {9 This. Message = "\n\n\n Test: Delete file-fileutils.deletefile (String)";Ten This. Logger.debug ( This. message); One AFu =NewFileUtils (); - - This. FileName = "I:\\cnblogs\\sourcecode\\autouiselenium\\test-output\\file\\txtfilewrite.txt"; the - This. message = "File deletion failed." The file path is: "+ This. FileName; - -Assert.asserttrue ( This. Fu.deletefile ( This. FileName), This. message); +}
Test: File Delete Test source code
At this point, Java learning -012-file deletion instance and the source code 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 -012-File Delete instance and source code