📄 classlocation.java
字号:
/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2004
* </p>
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Properties;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
public class ClassLocation implements ConstString
{
/**
* 取得指定类所在目录,如果类在一个Jar文件中,则指的是这个Jar文件所在目录
*
* @param cls
* 指定类
* @return 指定类所在目录,如果没有找到该类返回空字符串
*/
public static String getClassDir(Class cls)
{
try
{
URL url = getClassURL(cls);
if (url == null)
{
return EMPTY_STRING;
}
String classpath = url.getPath();
int nl = cls.getName().length() + DOTCLASS.length();
int pl = classpath.length();
int i_cut_start = 0;
int i_cut_end = pl - nl;
classpath = classpath.substring(i_cut_start, i_cut_end);
if (url.getProtocol().equalsIgnoreCase(PROTOCOL_JAR))
{
pl = i_cut_end - 1;
classpath = classpath.substring(0, pl); // cut the last slash
if (classpath.startsWith(PROTOCOL_FILE + COLON))
{
i_cut_start = PROTOCOL_FILE.length() + COLON.length();
}
i_cut_end = classpath.lastIndexOf(SLASH);
classpath = classpath.substring(i_cut_start, i_cut_end);
}
if (!classpath.endsWith(SLASH))
{
classpath += SLASH;
}
return URLDecoder.decode(classpath, CSN_UTF_8);
}
catch(Exception e)
{
// Do nothing
}
return EMPTY_STRING;
}
/**
* 取得指定类所在目录或jar文件
*
* @param cls
* 指定类
* @return 指定类所在目录,如果没有找到该类返回空字符串
*/
public static String getClassPath(Class cls)
{
try
{
URL url = getClassURL(cls);
if (url == null)
{
return EMPTY_STRING;
}
String classpath = url.getPath();
int nl = cls.getName().length() + DOTCLASS.length();
int pl = classpath.length();
int i_cut_start = 0;
int i_cut_end = pl - nl;
classpath = classpath.substring(i_cut_start, i_cut_end);
if (classpath.startsWith(PROTOCOL_FILE + COLON))
{
i_cut_start = PROTOCOL_FILE.length() + COLON.length();
classpath = classpath.substring(i_cut_start);
}
if (classpath.endsWith(SLASH))
{
classpath = classpath.substring(0, classpath.length() - 1);
}
return URLDecoder.decode(classpath, CSN_UTF_8);
}
catch(Exception e)
{
// Do nothing
}
return EMPTY_STRING;
}
/**
* 取得指定类的存取位置URL
*
* @param cls
* 指定类
* @return 指定类所在存取位置URL,如果没有找到该类返回空
*/
public static URL getClassURL(Class cls)
{
try
{
ClassLoader cl = ClassLoader.getSystemClassLoader();
String classname = cls.getName();
String findresname = classname.replace('.', '/') + DOTCLASS;
URL url = cl.getResource(findresname);
if (url == null)
{
int i = classname.lastIndexOf(DOT);
findresname = classname.substring(i + 1) + DOTCLASS;
url = cls.newInstance().getClass().getResource(findresname);
}
return url;
}
catch(Exception e)
{
// Do nothing
}
return null;
}
/**
* 取得相对于指定class所在路径的文件的InputStream
*
* <pre>
*
*
*
*
* e.g.
* class = com.company.app.relate
* classpath = /www/WEB-INF/classes/com/company/app/relate.class
* relatefile = resource/readme.txt find the file
* path = /www/WEB-INF/classes/resource/readme.txt
* classpath = /www/WEB-INF/lib/aa.jar![com/company/app/relate.class]
* relatefile = resource/readme.txt
*
* find the file path
* first = /www/WEB-INF/lib/resource/readme.txt
* find the file path
* next = /www/WEB-INF/lib/aa.jar![resource/readme.txt]
*
*
*
*
*
* </pre>
*
* @param PathRelateClass
* @param FilePath
* @return
* @throws IOException
*/
public static InputStream getFileInputStream(Class PathRelateClass,
String RelateFilePath) throws IOException
{
InputStream is;
String s = getClassPath(PathRelateClass);
if (s.endsWith("!"))
{
// cut the jar file
int i = s.lastIndexOf(SLASH);
String filesamejardir = s.substring(0, i) + '/' + RelateFilePath;
if (new File(filesamejardir).exists())
{
return new FileInputStream(filesamejardir);
}
// is a jar file, cut the "!"
s = s.substring(0, s.length() - 1);
JarFile jf = new JarFile(s);
ZipEntry ze = jf.getEntry(RelateFilePath);
return jf.getInputStream(ze);
}
else
{
s = s + '/' + RelateFilePath;
return new FileInputStream(s);
}
}
public static InputStream getFileInputStream(String relateFilePath)
throws IOException
{
return getFileInputStream(ClassLocation.class, relateFilePath);
}
public static Properties loadProperties(String filename) throws IOException
{
Properties p = new Properties();
InputStream is = getFileInputStream(filename);
p.load(is);
return p;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -