ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

»ùÓÚluceneµÄ°¸Àý¿ª·¢£ºClassUtil & CharsetUtil(Ò»)
2015-07-20 17:23:14 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:3´Î
Tags£º»ùÓÚ lucene °¸Àý ¿ª·¢ ClassUtil & CharsetUtil
?


ÕâÆª²©¿ÍÖ÷Òª½éÉÜ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 A
Ê×Ò³ ÉÏÒ»Ò³ 1 2 ÏÂÒ»Ò³ βҳ 1/2/2
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºpoj-1789 Truck History ÏÂһƪ£ºc++ ¶·µØÖ÷·¢ÅƳÌÐò³õ¼¶£¨·ÖÅä·¢..

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)