一,获取src下面的配置文件信息

  1,结构图如下:

  

  

package com.binp.properties;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle; public class GetPropertiesValues { public static void main(String[] args) throws Exception { //1,第一种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
String path = GetPropertiesValues.class.getResource("/").getPath(); System.out.println(path); FileInputStream fInputStream = new FileInputStream(path+"pro.properties"); Properties properties = new Properties(); properties.load(fInputStream);
System.out.println(properties.getProperty("className")); //2,第二种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
InputStream iStream = GetPropertiesValues.class.getResourceAsStream("/pro.properties");
properties.load(iStream);
iStream.close(); System.out.println(properties.getProperty("method")); //3,第三种方法(此方法只能将配置文件放置在src目录下,不能放在包中)
String value = ResourceBundle.getBundle("pro").getString("admin"); System.out.println(value); } }

二,获取WebContent目录下的配置文件

  1,前提条件:是在tomcat启动的情况下:

@WebServlet("/testEvery")
public class testEveryServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public testEveryServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* 访问url:http://localhost:8080/demoProj/testEveryServlet
*/
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");
System.out.println("doGet读取到的/WEB-INF/config/sysconfig.properties:path:"+path);
String url = request.getSession().getServletContext().getRealPath("/WEB-INF/config/config.properties");
System.out.println("doGet读取到的/WEB-INF/config/config.properties:url:"+url);
/**
* 结果:
* doGet:path:D:\tomcat7\wtpwebapps\demoProj\config\sysconfig.properties
* doGet:url:D:\tomcat7\wtpwebapps\demoProj\WEB-INF\config\config.properties
*/
//只能获取src下面的
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/config/test.properties");
Properties prop = new Properties(); //map
prop.load(in);
String url1 = prop.getProperty("url");
System.out.println("获取到的url1:"+url1);//获取到的url1:www.baidu.com //不可获取
InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/config.properties");
Properties prop2 = new Properties(); //map
prop.load(in2);
String url2 = prop2.getProperty("url");
System.out.println("获取到的url2:"+url2);//获取到的url2:null //不可获取
InputStream in3 = this.getServletContext().getResourceAsStream("/webcontent.properties");
Properties prop3 = new Properties(); //map
prop.load(in3);
String url3 = prop3.getProperty("url");
System.out.println("获取到的url3:"+url3);//获取到的url3:null //不可获取
InputStream in4 = this.getServletContext().getResourceAsStream("/config/wcc.properties");
Properties prop4 = new Properties(); //map
prop.load(in4);
String url4 = prop4.getProperty("url");
System.out.println("获取到的url4:"+url4);//获取到的url4:null // 读取src下config包中的testJava.java
// InputStream in = ReadFile.class.getResourceAsStream("/config/testJava.java");//in为null
// byte[] a=new byte[100];
// in.read(a, 0, 900);
// System.out.println("读取src下config包中的testJava.java的输入流in的内容toString:"+in.toString());
// System.out.println("读取到的a:"+a);
String fileName3 = ReadFile.class.getResource("/config/test.properties").getFile();
System.out.println("读取src下config包中的test.properties:"+fileName3);
//输出:读取src下config包中的test.properties:/D:/tomcat7/wtpwebapps/demoProj/WEB-INF/classes/config/test.properties
// in.close(); // 读取src下 基名为myproperties的properties文件,获取其中name配置值
String value = ResourceBundle.getBundle("myproperties").getString("name");
System.out.println("获取到的myproperties.properties的值value:"+value);
//输出:获取到的myproperties.properties的值value:myname // 读取src下myproperties.properties
InputStream in1 = ReadFile.class.getResourceAsStream("/myproperties.properties");
Properties properties = new Properties();
properties.load(in1);
String value2 = properties.getProperty("name"); // 获得name属性
System.out.println("获取到的myproperties.properties的值value2:"+value2);
//获取到的myproperties.properties的值value2:myname //读取src下的
String sensitiveWordsServerPath1 = SysConfig.getSysParam("sensitiveWords_server_path1");
System.out.println("获取的sensitiveWordsServerPath1:"+sensitiveWordsServerPath1);
//获取的sensitiveWordsServerPath1:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx //读取src下的
String pp = prop("sensitiveWords_server_path1");
System.out.println("pp:"+pp);//pp:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx
} public String prop(String url){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/sysconfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("p:"+p);
return p.getProperty(url);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");
System.out.println("doPost:path:"+path);
} }

参考文档:https://blog.csdn.net/superit401/article/details/78206877

JAVA Web项目获取src和WebContent目录下的配置文件的更多相关文章

  1. 【转】Java Web 项目获取运行时路径 classpath

    Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...

  2. Maven项目中读取src/main/resources目录下的配置文件

    在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties, ...

  3. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件

    路径说明: 一.加载类目录下的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ap ...

  4. java web项目获取各种路径

    1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 ...

  5. Java web项目搭建系列之二 Jetty下运行项目

    在项目pom.xml文件中添加Jetty运行配置 在pom.xml文件project节点下插入如下代码: <build> <plugins> <plugin> &l ...

  6. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式

    前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...

  7. web项目部署以及放到ROOT目录下

    最近度过了一个国庆长假,好几天都没有写博客了! 发布这篇案例也是希望能帮助到像我一样的菜鸟o(* ̄︶ ̄*)o,百度上面的资料都不怎么全.也没有人说明注意事项.总是这篇说一点.那个人也说补一点,最后自己 ...

  8. java web项目获取项目路径

    注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...

  9. Java Web 项目获取运行时路径 classpath

    假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么java文件夹和resources文件夹在运行时就是class ...

随机推荐

  1. JS取出特定字符前后的字符串,针对一串字符里面的单个字符前后的字符串

    //针对一串自负里面的单个字符前后的字符串<!doctype html> <html> <head> <meta charset="utf-8&qu ...

  2. 编写高性能的javascript代码(持续更新)

    参考资料: Vanilla JS——世界上最轻量的JavaScript框架(没有之一) http://segmentfault.com/a/1190000000355277 探索高效jQuery的奥秘 ...

  3. CF1042F Leaf Sets (贪心+树上构造)

    题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数 手动yy的贪心竟然对的 对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶 ...

  4. [terry笔记]学校管理系统

    如下是要求: # 角色:学校.学员.课程.讲师# 要求:# 1. 创建北京.上海 2 所学校# 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上 ...

  5. 2015 Multi-University Training Contest 6 hdu 5361 In Touch

    In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  6. EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER

    EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER ...

  7. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...

  8. True or False? and WHY??? Java HashSet Contains

    import java.util.HashSet; public class MyClass { public String s; public MyClass(String s) { this.s ...

  9. m_Orchestrate learning system---四、多看参考文档很多事情很轻松就解决了

    m_Orchestrate learning system---四.多看参考文档很多事情很轻松就解决了 一.总结 一句话总结:多看参考文档啊 1.面包屑导航如何实现? 1 <ol class=& ...

  10. [雅礼NOIP2018集训] day6

    打满暴力好像是一种挑战,已经连续几天考试最后一个小时自闭了,因为自以为打完了暴力,然而,结果往往差强人意 大概是考试的策略有些问题 T1: 我们设$g[x]$为在x时取小于等于m个物品的最大价值,下面 ...