The source of this article mainly for the application of Java to obtain the file name and file directory source code and test source code. If there is any deficiency, please ask the great God to correct me, I appreciate it! The source code test pass date is:2015-2-3 00:02:27, please be aware.
Java gets the file name of the source code as follows:
1 /**2 * @function Get file name3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java getfname, 2015-2-2 23:25:00 Exp $6 * 7 * @paramfilefullname: File full path8 * 9 * @returnString file nameTen */ One Publicstring Getfname (String filefullname) { AFilefullname = (Filefullname = =NULL) ? "": Filefullname; - - if("". Equals (Filefullname)) { the return""; - } - -File f =NewFile (filefullname); + - if(F.isfile ()) { + returnf.getname (); A}Else { at return""; - } -}
get the source code for the file name from the full path of the file
Test to get the file name of the test source as follows:
1 /**2 * Test: Get file name3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java getfname, 2015-2-3 00:02:27 Exp $6 *7 */8 Public voidGetfname () {9Fu =NewFileUtils ();Ten One This. FileName = "I:\\cnblogs\\sourcecode\\autouiselenium\\test-output\\file\\txtfilewrite.txt"; AString expect_fname = "TxtfileWrite.txt"; - - This. Message = "Failed to get file name. The file path is: "+ This. FileName + "Expected Result:" + Expect_fname + ", the actual result is:" + This. Fu.getfname ( This. FileName); the -Assert.assertequals ( This. Fu.getfname ( This. FileName), Expect_fname, This. message); -}
test: Get the test source code for the file name
The source code for the Java fetch file directory is as follows:
1 /**2 * @function Get file path3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java getfpath, 2015-2-2 23:24:13 Exp $6 * 7 * @paramFileName: File full path8 * 9 * @returnString file pathTen */ One Publicstring Getfpath (string filename) { Afilename = (FileName = =NULL) ? "": filename; - - if("". Equals (filename) { the return""; - } - -File f =NewFile (filename); + - if(F.isfile ()) { + returnf.getparent (); A}Else { at return""; - } -}
get the source code for the file path from the full path of the file
Test to get the file directory test source as follows:
1 /**2 * Test: Get file directory3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java getfpath, 2015-2-3 00:02:00 Exp $6 *7 */8 Public voidGetfpath () {9Fu =NewFileUtils ();Ten One This. FileName = "I:\\cnblogs\\sourcecode\\autouiselenium\\test-output\\file\\txtfilewrite.txt"; AString Expect_fpath = "I:\\cnblogs\\sourcecode\\autouiselenium\\test-output\\file"; - - This. Message = "Failed to get file directory. The file path is: "+ This. FileName + "Expected Result:" + Expect_fpath + ", the actual result is:" + This. Fu.getfpath ( This. FileName); the -Assert.assertequals ( This. Fu.getfpath ( This. FileName), Expect_fpath, This. message); -}
test: Get the test source code for the file directory
At this point, Java learning -009-file name and path to get the instance and 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 -009-file name and path acquisition instance and source code