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下面:实现非常简单,好了不多说了放代码:
- package com.syj.util;
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- /**
- * <p>
- * Title:URL辅助工具类
- * </p>
- *
- * <p>
- * Copyright: 转载请注明出处http://blog.csdn.net/sunyujia/
- * </p>
- *
- * @author 孙钰佳
- * @main sunyujia@yahoo.cn
- * @date Sep 21, 2008 12:31:23 PM
- */
- public class URLUtil {
- /**
- *
- * Description:取得当前类所在的文件
- *
- * @param clazz
- * @return
- * @mail sunyujia@yahoo.cn
- * @since:Sep 21, 2008 12:32:10 PM
- */
- public static File getClassFile(Class clazz) {
- URL path = clazz.getResource(clazz.getName().substring(
- clazz.getName().lastIndexOf(".") + 1)
- + ".class");
- if (path == null) {
- String name = clazz.getName().replaceAll("[.]", "/");
- path = clazz.getResource("/" + name + ".class");
- }
- return new File(path.getFile());
- }
- /**
- *
- * Description:同getClassFile 解决中文编码问题
- *
- * @param clazz
- * @return
- * @mail sunyujia@yahoo.cn
- * @since:Sep 21, 2008 1:10:12 PM
- */
- public static String getClassFilePath(Class clazz) {
- try {
- return java.net.URLDecoder.decode(getClassFile(clazz)
- .getAbsolutePath(), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return "";
- }
- }
- /**
- *
- * Description:取得当前类所在的ClassPath目录
- *
- * @param clazz
- * @return
- * @mail sunyujia@yahoo.cn
- * @since:Sep 21, 2008 12:32:27 PM
- */
- public static File getClassPathFile(Class clazz) {
- File file = getClassFile(clazz);
- for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
- file = file.getParentFile();
- if (file.getName().toUpperCase().endsWith(".JAR!")) {
- file = file.getParentFile();
- }
- return file;
- }
- /**
- *
- * Description: 同getClassPathFile 解决中文编码问题
- *
- * @param clazz
- * @return
- * @mail sunyujia@yahoo.cn
- * @since:Sep 21, 2008 1:10:37 PM
- */
- public static String getClassPath(Class clazz) {
- try {
- return java.net.URLDecoder.decode(getClassPathFile(clazz)
- .getAbsolutePath(), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return "";
- }
- }
- public static void main(String[] args) throws UnsupportedEncodingException {
- System.out.println(getClassFilePath(URLUtil.class));
- System.out.println(getClassPath(URLUtil.class));
- }
- }
在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目录.
大家可以在任意类加载环境下进行测试
- path1
- 黑马程序员_基础加强path1
- Loadrunner11 path1--path4 各补丁更新说明
- 电机控制 学习文件 path1.c
- Bean named 'BService' is expected to be of type 'path1' but was actually of type 'path2'
- vs2015正式版,建立安卓工程报错:值不能为空,参数名:path1的错误解决
- 打印VO的SQL语句和绑定参数的代码片断
- 嵌入式新手指南
- 自己做了个密钥
- 把Android低版本(如1.5)工程迁移到高版本(如2.2)去
- Python 自动连接ssh
- path1
- 文荣:你在替下属“背猴子”吗
- 应用Gnuplot进行数据的可视化 Ubuntu
- 第一个 Ajax 入门实例
- 任正非:为轮值CEO鸣锣开道
- My weakness
- Java虚拟机KVM在嵌入式Linux上的移植(转)
- Activity跳转页面切换漂亮效果(转)
- JavaBean 成员命名规范