path1

来源:互联网 发布:免费股票交易软件下载 编辑:程序博客网 时间:2024/03/28 17:55
 

3.类.class.getResource("")

看上去这确实是个好方法,但是它的局限性在于如果类在jar中的话,那么在打jar包的时候需要将文件夹也一起打进去,否则会返回null,jar文件实际上就是zip文件,zip文件中:文件是文件,文件夹是文件夹,不是关联在一起的,很多开源的jar包就没有把目录打进去只打了classes文件,虽然你能够看到文件的目录层次结构,但是调用类.class.getResource("")会返回null的.因为文件的目录结构和文件夹本身是两回事.对于这个问题可追溯到我以前的一篇帖子http://topic.csdn.net/u/20080520/21/1dc25316-8316-46f8-904b-ded9c4b7587a.html

至于在web应用中取相对路径的方法我就不说了,因为他有局限性,我这里说的都是在任何环境下都可以使用的方法.

取相对路径最安全的的方法是什么呢?答案是取类本身在系统中存储的文件位置,然后根据包层次向上一直找到Classpath下面:实现非常简单,好了不多说了放代码:

  1. package com.syj.util;
  2. import java.io.File;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.URL;
  5. /**
  6.  * <p>
  7.  * Title:URL辅助工具类
  8.  * </p>
  9.  *
  10.  * <p>
  11.  * Copyright: 转载请注明出处http://blog.csdn.net/sunyujia/
  12.  * </p>
  13.  *
  14.  * @author 孙钰佳
  15.  * @main sunyujia@yahoo.cn
  16.  * @date Sep 21, 2008 12:31:23 PM
  17.  */
  18. public class URLUtil {
  19.     /**
  20.      *
  21.      * Description:取得当前类所在的文件
  22.      *
  23.      * @param clazz
  24.      * @return
  25.      * @mail sunyujia@yahoo.cn
  26.      * @since:Sep 21, 2008 12:32:10 PM
  27.      */
  28.     public static File getClassFile(Class clazz) {
  29.         URL path = clazz.getResource(clazz.getName().substring(
  30.                 clazz.getName().lastIndexOf(".") + 1)
  31.                 + ".class");
  32.         if (path == null) {
  33.             String name = clazz.getName().replaceAll("[.]""/");
  34.             path = clazz.getResource("/" + name + ".class");
  35.         }
  36.         return new File(path.getFile());
  37.     }
  38.     /**
  39.      *
  40.      * Description:同getClassFile 解决中文编码问题
  41.      *
  42.      * @param clazz
  43.      * @return
  44.      * @mail sunyujia@yahoo.cn
  45.      * @since:Sep 21, 2008 1:10:12 PM
  46.      */
  47.     public static String getClassFilePath(Class clazz) {
  48.         try {
  49.             return java.net.URLDecoder.decode(getClassFile(clazz)
  50.                     .getAbsolutePath(), "UTF-8");
  51.         } catch (UnsupportedEncodingException e) {
  52.             e.printStackTrace();
  53.             return "";
  54.         }
  55.     }
  56.     /**
  57.      *
  58.      * Description:取得当前类所在的ClassPath目录
  59.      *
  60.      * @param clazz
  61.      * @return
  62.      * @mail sunyujia@yahoo.cn
  63.      * @since:Sep 21, 2008 12:32:27 PM
  64.      */
  65.     public static File getClassPathFile(Class clazz) {
  66.         File file = getClassFile(clazz);
  67.         for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
  68.             file = file.getParentFile();
  69.         if (file.getName().toUpperCase().endsWith(".JAR!")) {
  70.             file = file.getParentFile();
  71.         }
  72.         return file;
  73.     }
  74.     /**
  75.      *
  76.      * Description: 同getClassPathFile 解决中文编码问题
  77.      *
  78.      * @param clazz
  79.      * @return
  80.      * @mail sunyujia@yahoo.cn
  81.      * @since:Sep 21, 2008 1:10:37 PM
  82.      */
  83.     public static String getClassPath(Class clazz) {
  84.         try {
  85.             return java.net.URLDecoder.decode(getClassPathFile(clazz)
  86.                     .getAbsolutePath(), "UTF-8");
  87.         } catch (UnsupportedEncodingException e) {
  88.             e.printStackTrace();
  89.             return "";
  90.         }
  91.     }
  92.     public static void main(String[] args) throws UnsupportedEncodingException {
  93.         System.out.println(getClassFilePath(URLUtil.class));
  94.         System.out.println(getClassPath(URLUtil.class));
  95.     }
  96. }

 

在eclipse下执行

输出为

D:/SYJ.WORK/SYJ.WORKSPACE/ws1/util/classes/com/syj/util/URLUtil.class
D:/SYJ.WORK/SYJ.WORKSPACE/ws1/util/classes
打成jar包后在桌面下执行

输出为
file:/C:/Documents and Settings/Administrator/桌面/util.jar!/com/syj/util/URLUtil.class
file:/C:/Documents and Settings/Administrator/桌面

所取得的路径一直都是class文件的classpath目录.

大家可以在任意类加载环境下进行测试