一、路径

  绝对路径: 指的是文件在系统中的真实路径(物理路径)。
  相对路径: 指的是文件相对某个目录的相对路径。
  对于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工程中的相关路径的更多相关文章

  1. Log4j在Java工程中使用方法

    Eclipse新建Java工程,工程目录如下 1.下载log4j的Jar包,在Java工程下新建lib文件夹,将jar包拷贝到此文件夹,并将其加入到路径中,即:Jar包上右键——Build Path— ...

  2. java工程中不能存在多个数据库连接jar包

    java工程中不能存在多个数据库连接jar包 比如存在mysql-java-connector.jar的,放入mssqlserver.jar就会产生冲突.只能存在一个类型的jar包.

  3. 在java工程中导入jar包的注意事项

    在java工程中导入jar包后一定要bulid path,不然jar包不可以用.而在java web工程中导入jar包后可以不builld path,但最好builld path.

  4. java工程中的.classpathaaaaaaaaaaaaaaaa<转载>

    第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...

  5. java工程中的.classpath<转载>

    第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...

  6. 【技巧】Java工程中的Debug信息分级输出接口

    也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...

  7. 【技巧】Java工程中的Debug信息分级输出接口及部署模式

    也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...

  8. web工程中的各种路径(eclipse开发)

    目前遇到的 web 工程中要写url和路径的文件有 webContent中.jsp/.html :action src中的servlet类 : 映射地址.重定向.请求转发.访问资源文件(webCont ...

  9. Java工程中各种带有O的对象分类笔记

    在Java工程里面,我们总会碰到各种不同的带有O的对象, 对于一个小白来说,经常会混淆这些对象的使用场景,所以在这里mark一下,让自己的代码更加规范,但这个也是Java被诟病的地方,不同的业务需要给 ...

随机推荐

  1. 请求参数到表述层的类型转换——Struts2

    一.简介 说明:HTTP 协议传输数据没有类型的概念,在服务器端是通过 request.getParameter().request.getParameterValue() 方法得到请求参数为 Str ...

  2. P6 EPPM手动安装指南(Oracle数据库)(一)

    P6 EPPM手动安装指南(Oracle数据库) P6 EPPM Manual Installation Guide (Oracle Database) 1.      内容... 1 1.1.    ...

  3. C# Form实现自定义光标

    WinForm代码如下: using System; using System.Reflection; using System.Runtime.InteropServices; using Syst ...

  4. [水]用vb写了个PCB

    这学期我们学操作系统,所以得写个PCB. 于是我借鉴了一下windows的PCB,写了这个 Imports System.Runtime.InteropServices ''' <summary ...

  5. WPF listbox UI虚拟化

    ListBox  默认是UI虚拟化的. 1. 原生使用  <ListBox VirtualizingPanel.IsVirtualizing="True" Virtualiz ...

  6. MySQL Query Profile

    MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用量, 执行过程 System lock, Table lock 花多少时间等等.从 ...

  7. php中的字符串常用函数(四) ord() 获得字符的ascii码 chr()获取ascii码对应的字符

    ord('a');//=>97 返回小写a 的ascii码值97 chr(97);//=>a 返回ascii码表上的97对应的 小写a

  8. js 倒计时 跳转

    1. setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code ...

  9. Linux Shell系列教程之(十三)Shell分支语句case … esac教程

    本文是Linux Shell系列教程的第(十三)篇,更多Linux Shell教程请看:Linux Shell系列教程 分支语句非常实用,基本上高级语言都支持分支语句(python 没有),大多数都使 ...

  10. jar包和war包的区别(转)

    jar包和war包的区别:war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运 ...