基于lucene的案例开发:ClassUtil & CharsetUtil

释放双眼,带上耳机,听听看~!

 
这篇博客主要介绍ClassUtil类和CharsetUtil类。这两个也是项目中比较常用的类,一个用于指定文件路径,一个用于检测文件的编码方式。
ClassUtil
ClassUtil类中的方法主要是返回class文件所在的文件目录或工程的根目录地址,这主要用于指定工程中配置文件的路径,不至于环境迁移而导致配置文件路径错误。源代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1/**
2 * @Description:  类工具
3 */
4package com.lulei.util;
5
6public 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}
48

1
2
1   main函数运行结果如下:  
2

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
1/**  
2 *@Description:  编码方式检测类  
3 */
4package com.lulei.util;  
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.URL;
9import java.nio.charset.Charset;
10
11import info.monitorenter.cpdetector.io.ASCIIDetector;
12import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
13import info.monitorenter.cpdetector.io.JChardetFacade;
14import info.monitorenter.cpdetector.io.ParsingDetector;
15import info.monitorenter.cpdetector.io.UnicodeDetector;
16  
17public 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("https://www.csdn.net");
84      System.out.println(CharsetUtil.getStreamCharset(url, "default"));
85  }
86}
87

1
2
1   main函数运行结果如下:  
2

![](http://img.5iqiqu.com/images4/35/35ac56c501bf05923f8f647198967ecc.png)
ps:最近发现其他网站可能会对博客转载,上面并没有源链接,如想查看更多关于 基于lucene的案例开发 请点击这里。或访问网址www.2cto.com

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索