java项目读取properties路径问题

java项目读取properties路径问题

1.项目结构



2.方法一

/**
* 传统方式
*/
private void test1() throws FileNotFoundException, IOException {
		// FileInputStream in = new FileInputStream("src/main/resources/properties/url.properties"); // 错误写法1  java项目中这么写是正确的,web项目不行
		 //  FileInputStream in = new FileInputStream("classes/test.properties");//错误写法2
		     FileInputStream in = new FileInputStream("test.properties");//把test.properties文件copy到tomcate的bin目录下
		     Properties prop = new Properties();
		     prop.load(in);
		     String driver = prop.getProperty("driver");
		     String url = prop.getProperty("url");
		     System.out.println("driver-==-="+driver);
		     System.out.println("url-==-="+url);
	}

解析:
错误写法1:要是java项目,这么写是正确的,可以读到配置文件,但是web项目就会报找不到路径的错误,因为web项目会把test.properties文件编译到WEB-INF目录下的classes文件夹下。
错误写法2:这么看似没问题了,但是会报跟1一样的错误,因为这是相对路径,谁调用此类就是相对于谁,故这是相对于java虚拟机,本项目时用tomcate启动的java虚拟机,故回去tomcate的bin目录下去找classes/test.properties文件,找不到故报错


3.方法二

private void test2() throws IOException {
		System.out.println("test2");
		InputStream in = this.getServletContext().getResourceAsStream("WEB-INF/classes/test.properties");
		 Properties prop = new Properties();
		 prop.load(in);
		 String driver = prop.getProperty("driver");
		 String url = prop.getProperty("url");
		 System.out.println("driver-==-="+driver);
		 System.out.println("url-==-="+url);
	}


4.方法三

private void test3() throws FileNotFoundException, IOException {
		String path = this.getServletContext().getRealPath("WEB-INF/classes/test.properties");//拿到资源在硬盘上的路径
		FileInputStream in = new FileInputStream(path);
		 System.out.println("test3");
		 Properties prop = new Properties();
		 prop.load(in);
		 String driver = prop.getProperty("driver");
		 String url = prop.getProperty("url");
		 System.out.println("driver-==-="+driver);
		 System.out.println("url-==-="+url);
	}


5.方法四

private void test4() throws IOException {
		/**
		 * 读取webroot目录下的配置文件
		 */
		 System.out.println("test4");
		 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/test11.properties");
		 Properties prop = new Properties();
		 prop.load(in);
		 String driver = prop.getProperty("driver");
		 String url = prop.getProperty("url");
		 System.out.println("driver-==-="+driver);
		 System.out.println("url-==-="+url);
	}


6.方法五

private void test5() throws IOException {
		/**
		 * 类装载器方式读取,我常用的方式
		 */
		InputStream in  = readProp.class.getClassLoader().getResourceAsStream("test.properties");
		System.out.println("类装载器方式");
		 Properties prop = new Properties();
		 prop.load(in);
		 String driver = prop.getProperty("driver");
		 String url = prop.getProperty("url");
		 System.out.println("driver-==-="+driver);
		 System.out.println("url-==-="+url);
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值