1F Java bean类中得到classes文件夹的物理路径 唧唧 Post by : 2009-09-10 16:06:29.0
java代码
- import java.io.IOException;
- import java.net.URL;
-
- public class FilePath {
-
-
-
-
-
- public static void main(String[] args) throws IOException {
-
- System.out.println(new FilePath().getClassPath());
-
- }
-
- private String getClassPath() {
- String strClassName = getClass().getName();
- String strPackageName = "";
- if (getClass().getPackage() != null) {
- strPackageName = getClass().getPackage().getName();
- }
-
- String strClassFileName = "";
- if (!"".equals(strPackageName)) {
- strClassFileName = strClassName.substring(strPackageName.length() + 1, strClassName.length());
- } else {
- strClassFileName = strClassName;
- }
-
- URL url = getClass().getResource(strClassFileName + ".class");
- String strURL = url.toString();
-
- int n = strURL.indexOf("/classes/");
-
- strURL = strURL.substring(strURL.indexOf("/") + 1, n);
-
- strURL = strURL.replaceAll("%20", " ");
- return strURL;
- }
-
- }
|