分享

基于lucene的案例开发11:项目常用类ClassUtil & CharsetUtil介绍

本帖最后由 nettman 于 2015-4-16 22:40 编辑

问题导读

1.ClassUtil类中的方法的作用是什么?
2.CharsetUtil类工具类用途是什么?
3.如何使用第三方jar包检测文件的编码?






接上篇
基于lucene的案例开发10:搜索后台基础,JsonUtil & XmlUtil类介绍


  这篇博客主要介绍ClassUtil类和CharsetUtil类。这两个也是项目中比较常用的类,一个用于指定文件路径,一个用于检测文件的编码方式。


ClassUtil
      ClassUtil类中的方法主要是返回class文件所在的文件目录或工程的根目录地址,这主要用于指定工程中配置文件的路径,不至于环境迁移而导致配置文件路径错误。源代码如下:

  1. /**
  2. * @Description:  类工具
  3. */  
  4. package com.lulei.util;  
  5.   
  6. public class ClassUtil {  
  7.       
  8.     /**
  9.      * @param c
  10.      * @return
  11.      * @Description: 返回类class文件所在的目录
  12.      */  
  13.     public static String getClassPath(Class<?> c) {  
  14.         return c.getResource("").getPath().replaceAll("%20", " ");  
  15.     }  
  16.       
  17.     /**
  18.      * @Description:  
  19.      * @param c
  20.      * @param hasName 是否显示文件名
  21.      * @return 返回类class文件的地址
  22.      */  
  23.     public static String getClassPath(Class<?> c, boolean hasName) {  
  24.         String name = c.getSimpleName() + ".class";  
  25.         String path = c.getResource(name).getPath().replaceAll("%20", " ");  
  26.         if (hasName) {  
  27.             return path;  
  28.         } else {  
  29.             return path.substring(0, path.length() - name.length());  
  30.         }  
  31.     }  
  32.       
  33.     /**
  34.      * @Description: 返回类class文件所在的顶级目录
  35.      * @param c
  36.      * @return
  37.      */  
  38.     public static String getClassRootPath(Class<?> c) {  
  39.         return c.getResource("/").getPath().replaceAll("%20", " ");  
  40.     }  
  41.       
  42.     public static void main(String[] args) {  
  43.         System.out.println(ClassUtil.getClassPath(ClassUtil.class, true));  
  44.         System.out.println(ClassUtil.getClassPath(Math.class, true));  
  45.         System.out.println(ClassUtil.getClassRootPath(Math.class));  
  46.     }  
  47. }  
复制代码




      main函数运行结果如下:
1.png

CharsetUtil
      CharsetUtil类是基于cpdetector第三方jar包实现的编码检测工具类。如果接触过实际项目,你绝对会碰到程序读取文件乱码或更新运营文件网站就无法正常显示等一系列问题,而这些问题多数都是因为文件编码问题导致的。当然这个工具类,在下一部分的爬虫程序中也扮演着重要的角色。源程序如下:

  1. /**   
  2. *@Description:  编码方式检测类   
  3. */   
  4. package com.lulei.util;   
  5.   
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.net.URL;  
  9. import java.nio.charset.Charset;  
  10.   
  11. import info.monitorenter.cpdetector.io.ASCIIDetector;  
  12. import info.monitorenter.cpdetector.io.CodepageDetectorProxy;  
  13. import info.monitorenter.cpdetector.io.JChardetFacade;  
  14. import info.monitorenter.cpdetector.io.ParsingDetector;  
  15. import info.monitorenter.cpdetector.io.UnicodeDetector;  
  16.    
  17. public class CharsetUtil {  
  18.     private static final CodepageDetectorProxy detector;  
  19.       
  20.     static {//初始化探测器  
  21.         detector = CodepageDetectorProxy.getInstance();  
  22.         detector.add(new ParsingDetector(false));  
  23.         detector.add(ASCIIDetector.getInstance());  
  24.         detector.add(UnicodeDetector.getInstance());  
  25.         detector.add(JChardetFacade.getInstance());  
  26.     }  
  27.   
  28.     /**
  29.      * @param url
  30.      * @param defaultCharset
  31.      * @Author:lulei   
  32.      * @return 获取文件的编码方式
  33.      */  
  34.     public static String getStreamCharset (URL url, String defaultCharset) {  
  35.         if (url == null) {  
  36.             return defaultCharset;  
  37.         }  
  38.         try {  
  39.             //使用第三方jar包检测文件的编码  
  40.             Charset charset = detector.detectCodepage(url);  
  41.             if (charset != null) {  
  42.                 return charset.name();  
  43.             }  
  44.         } catch (Exception e1) {  
  45.             // TODO Auto-generated catch block  
  46.             e1.printStackTrace();  
  47.         }  
  48.         return defaultCharset;  
  49.     }  
  50.       
  51.     /**
  52.      * @param inputStream
  53.      * @param defaultCharset
  54.      * @return
  55.      * @Author:lulei   
  56.      * @Description: 获取文件流的编码方式
  57.      */  
  58.     public static String getStreamCharset (InputStream inputStream, String defaultCharset) {  
  59.         if (inputStream == null) {  
  60.             return defaultCharset;  
  61.         }  
  62.         int count = 200;  
  63.         try {  
  64.             count = inputStream.available();  
  65.         } catch (IOException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.         try {  
  70.             //使用第三方jar包检测文件的编码  
  71.             Charset charset = detector.detectCodepage(inputStream, count);  
  72.             if (charset != null) {  
  73.                 return charset.name();  
  74.             }  
  75.         } catch (Exception e1) {  
  76.             // TODO Auto-generated catch block  
  77.             e1.printStackTrace();  
  78.         }  
  79.         return defaultCharset;  
  80.     }  
  81.       
  82.     public static void main(String[] args) throws Exception {  
  83.         URL url = new URL("http://www.csdn.net");  
  84.         System.out.println(CharsetUtil.getStreamCharset(url, "default"));  
  85.     }  
  86. }  
复制代码




      main函数运行结果如下:

2.png



相关内容:
基于lucene的案例开发1:lucene初始认知

基于lucene的案例开发2:索引数学模型

基于lucene的案例开发3:索引文件结构

基于lucene的案例开发4:创建索引

基于lucene的案例开发5:搜索索引

基于lucene的案例开发6:分词器介绍

基于lucene的案例开发7:Query查询

基于lucene的案例开发8:IndexSearcher中检索方法

基于lucene的案例开发9:案例初识

基于lucene的案例开发10:搜索后台基础,JsonUtil & XmlUtil类介绍

基于lucene的案例开发11:项目常用类ClassUtil & CharsetUtil介绍

基于lucene的案例开发12:数据库连接池

基于lucene的案例开发13:实现实时索引基本原理

基于lucene的案例开发14:实时索引管理类IndexManager

基于lucene的案例开发15:实时索引的检索

基于lucene的案例开发16:实时索引的修改

基于lucene的案例开发17:查询语句创建PackQuery

基于lucene的案例开发18:纵横小说更新列表页抓取

基于lucene的案例开发19:纵横小说简介页采集

基于lucene的案例开发20:纵横小说章节列表采集

基于lucene的案例开发21:纵横小说阅读页采集

http://blog.csdn.net/xiaojimanman/article/category/2841877

加微信w3aboutyun,可拉入技术爱好者群

已有(2)人评论

跳转到指定楼层
yepiaochen 发表于 2015-4-13 09:06:03
好东西,要学习。。
回复

使用道具 举报

西山东 发表于 2015-4-13 13:01:03
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条