Title: 在类中取得当前文件所在的相对路径与绝对路径
* *Description: 在类中取得当前文件所在的相对路径与绝对路径
* *Copyright: Copyright (c) 1997 - 2005
* * * * * @version 1.0 */ public class GetClassPath { public GetClassPath() { super(); } /** * main * * @param args String[] */ public static void main(String[] args) { GetClassPath getclasspath = new GetClassPath(); System.out.println(getclasspath.getClassPath()); } /** * 在类中取得当前文件所在的相对路径与绝对路径 * * @return String */ public String getClassPath() { String strClassName = getClass().getName(); String strPackageName = ""; if(getClass().getPackage() != null) { strPackageName = getClass().getPackage().getName(); } System.out.println("ClassName:" + strClassName); System.out.println("PackageName:" + strPackageName); String strClassFileName = ""; if(!"".equals(strPackageName)) { strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length()); } else { strClassFileName = strClassName; } System.out.println("ClassFileName:" + strClassFileName); URL url = null; url = getClass().getResource(strClassFileName + ".class"); String strURL = url.toString(); strURL = strURL.substring(strURL.indexOf('/') + 1,strURL.lastIndexOf('/')); return strURL; } }本文地址:http://com.8s8s.com/it/it11414.htm