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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/43194793

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

ClassUtil

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

/**
 * @Description:  类工具
 */
package com.lulei.util;

public class ClassUtil {

	/**
	 * @param c
	 * @return
	 * @Description: 返回类class文件所在的目录
	 */
	public static String getClassPath(Class<?> c) {
		return c.getResource("").getPath().replaceAll("%20", " ");
	}

	/**
	 * @Description:
	 * @param c
	 * @param hasName 是否显示文件名
	 * @return 返回类class文件的地址
	 */
	public static String getClassPath(Class<?> c, boolean hasName) {
		String name = c.getSimpleName() + ".class";
		String path = c.getResource(name).getPath().replaceAll("%20", " ");
		if (hasName) {
			return path;
		} else {
			return path.substring(0, path.length() - name.length());
		}
	}

	/**
	 * @Description: 返回类class文件所在的顶级目录
	 * @param c
	 * @return
	 */
	public static String getClassRootPath(Class<?> c) {
		return c.getResource("/").getPath().replaceAll("%20", " ");
	}

	public static void main(String[] args) {
		System.out.println(ClassUtil.getClassPath(ClassUtil.class, true));
		System.out.println(ClassUtil.getClassPath(Math.class, true));
		System.out.println(ClassUtil.getClassRootPath(Math.class));
	}
}

main函数运行结果如下:

CharsetUtil

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

 /**
 *@Description:  编码方式检测类
 */
package com.lulei.util;  

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

public class CharsetUtil {
	private static final CodepageDetectorProxy detector;

	static {//初始化探测器
		detector = CodepageDetectorProxy.getInstance();
		detector.add(new ParsingDetector(false));
		detector.add(ASCIIDetector.getInstance());
		detector.add(UnicodeDetector.getInstance());
		detector.add(JChardetFacade.getInstance());
	}

	/**
	 * @param url
	 * @param defaultCharset
	 * @Author:lulei
	 * @return 获取文件的编码方式
	 */
	public static String getStreamCharset (URL url, String defaultCharset) {
		if (url == null) {
			return defaultCharset;
		}
		try {
			//使用第三方jar包检测文件的编码
			Charset charset = detector.detectCodepage(url);
			if (charset != null) {
				return charset.name();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return defaultCharset;
	}

	/**
	 * @param inputStream
	 * @param defaultCharset
	 * @return
	 * @Author:lulei
	 * @Description: 获取文件流的编码方式
	 */
	public static String getStreamCharset (InputStream inputStream, String defaultCharset) {
		if (inputStream == null) {
			return defaultCharset;
		}
		int count = 200;
		try {
			count = inputStream.available();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			//使用第三方jar包检测文件的编码
			Charset charset = detector.detectCodepage(inputStream, count);
			if (charset != null) {
				return charset.name();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return defaultCharset;
	}

	public static void main(String[] args) throws Exception {
		URL url = new URL("http://www.csdn.net");
		System.out.println(CharsetUtil.getStreamCharset(url, "default"));
	}
}

main函数运行结果如下:

ps:最近发现其他网站可能会对博客转载,上面并没有源链接,如想查看更多关于基于lucene的案例开发点击这里。或访问网址http://blog.csdn.net/xiaojimanman/article/category/2841877

时间: 2024-08-04 22:18:14

基于lucene的案例开发:ClassUtil & CharsetUtil的相关文章

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/43272993 通过java程序去连接数据库时,使用的协议是TCP/IP协议,TCP/IP协议需要进行3次握手.如果每一次数据库操作都需要创建一个新的连接,都要进行3次握手,这是十分浪费资源的,程序的效率也不是很高.为了解决这个问题,我们想可不可以自己维护一些数据库连接,需要数据库操作的时候,直接使用这其中的一个连接,用完了,在还给它,这样的话就不需要每次数据库操作都创建一个新的

基于lucene的案例开发:JsonUtil &amp; XmlUtil

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/43194015 从这篇博客开始第二大部分就算正式开始了,不过在介绍搜索后台之前,还是先介绍写可能使用的大工具类,这样在后面的搜索后台介绍中,就不会穿插其他的内容介绍.这篇就主要介绍两个工具类:json.xml格式数据处理类. JSON 在前后台数据通信过程中,json数据格式是一种比较常用的方式.将javabean转化为json格式字符串,可以通过简单的字符串拼接,也可以使用第三

基于lucene的案例开发:ParseUtil &amp; ParseRequest

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/43195045 这篇博客主要介绍ParseUtil类和ParseRequest类,因为这两个类都比较简单,所以这里就不会给出事例程序. ParseUtil ParseUtil类主要实现将字符串(数字)转化为数值,这个在读取配置文件或数据转化过程中有很大的作用.源程序如下: /** *@Description: 转换类 */ package com.lulei.util; publ

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44279753 http://www.llwjy.com/blogdetail/31bb705106379feaf6d31b58dd777be6.html 个人博客小站搭建成功,网址 www.llwjy.com,欢迎大家来吐槽~ 在前面的博客中,我们已经介绍了IndexSearcher中的检索方法,也介绍了如何基于lucene中的NRT*类去创建实时索引,在这篇博客中我们就重点介

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44280311 http://www.llwjy.com/blogdetail/e42fa5c3097f4964fca0fdfe7cd7a9a2.html 个人的博客小站已经上线了,网址 www.llwjy.com,欢迎大家来吐槽~ 上一篇博客已经介绍了实时索引的检索功能,这个就相当于数据的的查询功能,我们都知道数据库的增删改查是最常用的四个功能,实时索引也是一样,他也有增删改查

基于lucene的案例开发:纵横小说分布式采集

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/46812645 http://www.llwjy.com/blogdetail/9df464b20cca5405c7ce07e2fb2d768f.html 个人博客站已经上线了,网址 www.llwjy.com ~欢迎各位吐槽~ ----------------------------------------------------------------------------

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/43192055 首先抱歉,这几天在准备案例的整体框架设计,所以更新就断了几天,还请原谅. 案例整体介绍 在我们开始正式的案例开发介绍之前,我们先看一下整体的案例demo介绍,明白案例是做什么的. 从上图中,我们可以看出,这个案例主要是通过爬虫程序去采集纵横小说上的资源,然后将资源存储到自己的数据库中,将数据库中的需要检索的数据通过lucene建立索引文件,最后通过web服务展示数

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44656141 http://www.llwjy.com/blogdetail/162e5e70516d7ddfb6df8f77e6b13a2b.html 个人博客站已经上线了,网址 www.llwjy.com~欢迎各位吐槽 ------------------------------------------------------------------------------

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

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44937073 http://www.llwjy.com/blogdetail/29bd8de30e8d17871c707b76ec3212b0.html 个人博客站已经上线了,网址 www.llwjy.com ~欢迎各位吐槽~ ----------------------------------------------------------------------------