web项目中路径的获取
来源:互联网 发布:java编程语言培训 编辑:程序博客网 时间:2024/06/05 06:57
String path = (getClass().getClassLoader().getResource("").toURI()).getPath();System.out.println(getClass());//获取当前类 System.out.println(WebConfigListener.class);//指定当前类 System.out.println(getClass().getClassLoader()); //获取类加载器 其作用是加载类动jvm中System.out.println(getClass().getClassLoader().getResource("")); //获取当前类的所在的classes目录(URL)System.out.println(getClass().getClassLoader().getResource("").toURI()); //将URL转换成URISystem.out.println(path);
输出结果如下:
class com.right.listener.WebConfigListenerclass com.right.listener.WebConfigListenerweblogic.utils.classloaders.ChangeAwareClassLoader@1129103 finder: weblogic.utils.classloaders.CodeGenClassFinder@16708cc annotation: _appsdir_SpringMvcWeb_dir@SpringMvcWebfile:/D:/bea/user_projects/domains/web_domain/autodeploy/SpringMvcWeb/WEB-INF/classes/file:/D:/bea/user_projects/domains/web_domain/autodeploy/SpringMvcWeb/WEB-INF/classes//D:/bea/user_projects/domains/web_domain/autodeploy/SpringMvcWeb/WEB-INF/classes/
将项目中的资源文件读取并存到流中:
InputStream is=this.getClass().getClassLoader().getResourceAsStream("config.properties");
关于ClassLoader,我们用到最多的几个方法:
public URL getResource(String name)查找具有给定名称的资源。资源是可以通过类代码以与代码基无关的方式访问的一些数据(图像、声音、文本等)。
资源名称是以 '/' 分隔的标识资源的路径名称。
public InputStream getResourceAsStream(String name)返回读取指定资源的输入流。
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- 数据库设计方法、规范与技巧 1
- oracle中用dom拼写XML树
- 常见希腊字母读法
- C51创建一个工程模版
- 算法导论 2.2-2
- web项目中路径的获取
- 【Java设计模式】之组合模式
- 求阶乘的几种方法
- 根据状态个数来决定总设备的状态(主要是循环loop)
- linux内核移植x86平台
- The MSI Driver Guide HOWTO
- 地区选择JS 三级联动
- 字节对齐
- app开发接单--app开发项目案例