java工程中的相关路径
一、路径
绝对路径: 指的是文件在系统中的真实路径(物理路径)。
相对路径: 指的是文件相对某个目录的相对路径。
对于java application 工程来说,当编写完一个类之后,class文件会编译,默认存放在bin目录中。
存放的目录结构安装包的命名依次存放。同时一些在src目录,或其他Source Fodder目录中的资源文件也会被编译到bin目录中(有些构建的不是存放在bin目录中,这根据构建工具而异)。
而当程序运行的时候,执行的就是bin目录中的class文件,读取的资源文件也是bin目录中的。
对于java web 工程来说,当编写完一个类之后,class文件默认会被编译到这个web工程下的Webapp目录下的WEB-INF生成的classes目录中。
存放的目录结构安装包的命名依次存放。同样一些在src目录,或其他Source Fodder目录中的资源文件也会被编译到classes目录中。
而我们对路径的操作基本上是基于class文件所在的目录进行的。
二、示例
1、通过class 与 ClassLoader获取路径
//testapp为工程名称
// 返回编译后的class-path路径
//路径: /D:/web-create/testapp/target/classes/
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
//路径: /D:/web-create/testapp/target/classes/com 此处的com目录必须要存在,否则会抛异常
System.out.println(ClassLoader.getSystemResource("com").getPath());
//路径: /D:/web-create/testapp/target/classes/ 编译代码后的classes路径
System.out.println(ClassLoader.getSystemResource("").getPath());
//路径: /D:/web-create/testapp/target/classes/ 编译代码后的classes路径
System.out.println(Test.class.getResource("/").getPath());
//路径: /D:/web-create/testapp/target/classes/com/ Test.class文件所在路径
System.out.println(Test.class.getResource("").getPath());
//路径: /D:/web-create/testapp/target/classes/com/Test.class Test.class文件全路径
System.out.println(Test.class.getResource("Test.class").getPath());
2、webapp中通过request,session等获取路径
HttpServletRequest request = null;
///返回web应用名称: testapp
System.out.println(request.getContextPath()); // 当前web应用的绝对路径(后面没有\) E:/web-create\testapp\src\main\resources\webapp
System.out.println(request.getSession().getServletContext().getRealPath("")); // 当前web应用的绝对路径(后面有\) E:/web-create\testapp\src\main\resources\webapp\
System.out.println(request.getSession().getServletContext().getRealPath("/")); // 当前web应用中指定目录绝对路径 E:/web-create\testapp\src\main\resources\webapp\resource\image
System.out.println(request.getSession().getServletContext().getRealPath("/resource/image")); //获取PATH目录下的子目录以集合的形式返回():[/resource/image/2015/]
System.out.println(request.getSession().getServletContext().getResourcePaths("/resource/image")); // 当前web应用中指定目录绝对路径 E:/web-create\testapp\src\main\resources\webapp\resource\image
System.out.println(request.getRealPath("/resource/image"));// 不建议使用,用 ServletContext.getRealPath()方法代替
3、文件相关路径
//路径: D:\ 工程所在根目录
System.out.println(new File("/").getAbsolutePath());
//路径: D:\web-create\testapp\com
System.out.println(new File("com").getAbsolutePath());
//路径:D:\web-create\testapp
System.out.println(new File("").getAbsolutePath());
//路径:D:\web-create\testapp
System.out.println(System.getProperty("user.dir"));
// class path 路径
System.out.println(System.getProperty("java.class.path"));
4、加载src(实际上是bin目录或classes目录下的)下的properties
//加载文件相关
InputStream is = new FileInputStream(ClassLoader.getSystemResource("").getPath() + "config.redis.pool.properties");//该种方式与下面得到的结果一致
is = Test.class.getResourceAsStream("config.redis.pool.properties");//与下面结果一样
is = Test.class.getResourceAsStream("/config.redis.pool.properties");
//InputStream is = Test.class.getResourceAsStream("/com/config.redis.pool.properties");//参数是相对于classes目录下的文件全路径
Properties properties = new Properties();
properties.load(is);
is.close();
for(Object obj :properties.keySet()){
System.out.println("key: " + obj + " value: " + properties.get(obj));
}
java工程中的相关路径的更多相关文章
- Log4j在Java工程中使用方法
Eclipse新建Java工程,工程目录如下 1.下载log4j的Jar包,在Java工程下新建lib文件夹,将jar包拷贝到此文件夹,并将其加入到路径中,即:Jar包上右键——Build Path— ...
- java工程中不能存在多个数据库连接jar包
java工程中不能存在多个数据库连接jar包 比如存在mysql-java-connector.jar的,放入mssqlserver.jar就会产生冲突.只能存在一个类型的jar包.
- 在java工程中导入jar包的注意事项
在java工程中导入jar包后一定要bulid path,不然jar包不可以用.而在java web工程中导入jar包后可以不builld path,但最好builld path.
- java工程中的.classpathaaaaaaaaaaaaaaaa<转载>
第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...
- java工程中的.classpath<转载>
第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...
- 【技巧】Java工程中的Debug信息分级输出接口
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- 【技巧】Java工程中的Debug信息分级输出接口及部署模式
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- web工程中的各种路径(eclipse开发)
目前遇到的 web 工程中要写url和路径的文件有 webContent中.jsp/.html :action src中的servlet类 : 映射地址.重定向.请求转发.访问资源文件(webCont ...
- Java工程中各种带有O的对象分类笔记
在Java工程里面,我们总会碰到各种不同的带有O的对象, 对于一个小白来说,经常会混淆这些对象的使用场景,所以在这里mark一下,让自己的代码更加规范,但这个也是Java被诟病的地方,不同的业务需要给 ...
随机推荐
- 对象Transform,对属性赋值
private void ContructRequest(Dictionary<string, string> dictionary, CustomerSearchRequest requ ...
- IOS开发UI基础UIImageView属性属性
UIImageView属性 1.Image 设置图片,默认显示 UIImageView *_imageView = [[UIImageView alloc]init]; _imageView.imag ...
- OS X(EI Capitan)常用快捷键整理
刚上班就配了台RMBP,虽然触摸板确实好用,但是对鼠标的支持太差导致无法使用鼠标,而某些功能用触摸板还是不很方便 于是使用快捷键就势在必行了,参考了苹果官方文档和一些其他资料,整理出常用快捷键清单 准 ...
- Team Foundation Server简介
对于任何一个软件开发团队而言,成功的一个重要因素在于成员之间.成员与首先使用软件的用户之间有很好的沟通. Team Foundation Server是一个独立的服务器产品,逻辑上,由下列两层组成,这 ...
- springMVC中Dispatcher中的/和/*的区别
1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个Dispatc ...
- 重构第17天提取父类(Extract SuperClass)
今天的重构来自 Martin Fowler的http://refactoring.com/catalog/extractSuperclass.html. 理解:本文中的“提取父类”是指类中有一些字段或 ...
- WebApi 登录身份验证
前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个功能复杂的业务应用系统,通过角色授权来控制用户访问,本文通过Form认证,Mvc的Controller基类及Action的权 ...
- 缺少google api密钥,因此chromium的部分功能将无法使用”的解决办法
使用Chromium时会遇到 "缺少google api密钥,因此chromium的部分功能将无法使用"提示,google了一下 setx Google_API_K ...
- WinForm设置右键菜单
上一篇:设置窗体透明C#代码 如果要实现MDI窗体的右键菜单,可以添加一个contextMenuStrip(ID设置为:contextMenuStrip_hewenqi),然后设置窗体的Context ...
- 【iOS】通知监听
下例为:监听文本框 accountField 内容的改变, 当发生改变时, 调用textChange方法(多次).监听结束需要移除通知. - (void)viewDidLoad { [super ...