码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习-008-判断文件类型实例

时间:2015-03-09 23:57:40      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

此文源码主要为应用 Java 如何判断文件类型的源码及其测试源码。若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-2 23:02:00,请知悉。

Java 判断文件类型源码如下所示:

技术分享
 1     /**
 2      * @function 判断文件类型
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java assertFileType, 2014-11-25 17:49:36 Exp $
 6      * 
 7      * @param filename : 文件名
 8      * @param filetype : 文件类型:{EXCEL, CSV, XML, TXT, LOG, DBF, INI, WORD, POWERPOINT, PROPERTIES}
 9      * 
10      * @return boolean
11      */
12     public boolean assertFileType(String filename, String filetype){
13         boolean success = false;
14         
15         /* 文件扩展名:获取文件扩展名并转换为小写  */
16         String fileExtension = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
17         
18         if (!this.constantslist.FILETYPE.get(filetype).startsWith(fileExtension)) {
19             this.message = "参数非法:第二个参数{文件类型}非法, 文件类型{" + fileExtension + "}不在文件类型列表[" + this.constantslist.FILETYPE.toString() + "]中, 不支持!";
20             this.logger.error(this.message);
21         } else {
22             success = true;
23         }
24         
25         return success;
26     }
判断文件类型源码

测试源码如下所示:

技术分享
 1     /**
 2      * 测试:验证文件类型
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java assertFileType, 2015-2-2 23:02:00 Exp $
 6      */
 7     @Test
 8     public void assertFileType(){
 9         fu = new FileUtils();
10         String filename_ = "I:\\CNblogs\\sourceCode\\autoUISelenium\\test-output\\file\\txtfileWrite.txt";
11         String expected = "TXT";
12         
13         Assert.assertTrue(fu.assertFileType(filename_, expected), "文件类型校验失败");
14     }
测试:验证文件类型判断是否正确

至此, Java学习-008-判断文件类型实例 顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

Java学习-008-判断文件类型实例

标签:

原文地址:http://www.cnblogs.com/fengpingfan/p/4324851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!