java 中获取资源文件 以及 路径

新建一个Java工程,新建一个constants.properties资源文件

Java代码
1.userName = snail
2.age = 24 3.password = 123456 userName = snail
age = 24
password = 123456然后我们再建立一个类Constans.java,附上静态变量

Java代码
1.package testproperties;
2.
3.public class Constants { 4.
5. public static String userName; 6. public static int age; 7. public static String password; 8.}
package testproperties;

public class Constants {

public static String userName;
public static int age;
public static String password;
}接下来的工作就尝试着如何获取properties文件类定义的姓名、年龄和密码了,新建一个InitProperties类

Java代码
1.package testproperties;
2.
3.import java.io.FileInputStream; 4.import java.io.IOException; 5.import java.util.Properties; 6.
7.public class InitProperties { 8. private static final long serialVersionUID = -2106230733190196852L; 9.
10. public void init() 11. {
12. System.out.println("#############################加载配置信息###########################"); 13. Properties prop = new Properties(); 14.
15. //得到的是编译后的bin的目录Class.class.getClass().getResource("/").getPath(); 16.
17. //这个是绝对路径 18.// String filepath = "E:\\myeclipse6\\workspace\\XXX\\src\\testproperties\\constants.properties"; 19.
20. String filepath = Class.class.getClass().getResource("/").getPath()+"/testproperties/constants.properties" ; 21.
22. System.out.println("++++++++++++"+Class.class.getClass().getResource("/").getPath()+"+++++++++++++"); 23.
24. FileInputStream fis = null; 25. try { 26. fis = new FileInputStream(filepath); 27. prop.load(fis);
28. Constants.userName = prop.getProperty("userName"); 29. Constants.age = Integer.parseInt(prop.getProperty("age")); 30. Constants.password = prop.getProperty("password"); 31. System.out.println(Constants.userName+Constants.age+Constants.password);;
32. System.out.println("#############################加载配置信息完成###########################"); 33. }
34. catch (IOException e) { 35. System.out.println("加载constants.properties文件失败,文件不存在后者路径不正确! "); 36. e.printStackTrace();
37. }
38. }
39. public static void main(String[] args) { 40. InitProperties ip = new InitProperties(); 41. ip.init();
42. }
43.}
package testproperties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class InitProperties {
private static final long serialVersionUID = -2106230733190196852L;

public void init()
{
System.out.println("#############################加载配置信息###########################");
Properties prop = new Properties();

//得到的是编译后的bin的目录Class.class.getClass().getResource("/").getPath();

//这个是绝对路径
// String filepath = "E:\\myeclipse6\\workspace\\XXX\\src\\testproperties\\constants.properties";

String filepath = Class.class.getClass().getResource("/").getPath()+"/testproperties/constants.properties" ;

System.out.println("++++++++++++"+Class.class.getClass().getResource("/").getPath()+"+++++++++++++");

FileInputStream fis = null;
try {
fis = new FileInputStream(filepath);
prop.load(fis);
Constants.userName = prop.getProperty("userName");
Constants.age = Integer.parseInt(prop.getProperty("age"));
Constants.password = prop.getProperty("password");
System.out.println(Constants.userName+Constants.age+Constants.password);;
System.out.println("#############################加载配置信息完成###########################");
}
catch (IOException e) {
System.out.println("加载constants.properties文件失败,文件不存在后者路径不正确! ");
e.printStackTrace();
}
}
public static void main(String[] args) {
InitProperties ip = new InitProperties();
ip.init();
}
}


现在附上集中在jsp、Java、和servlet中获取路径的方法:(引用自http://zhidao.baidu.com/question/86179810.html?fr=qrl&cid=93&index=5)

1.jsp中取得路径:

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST



2.在类中取得路径:

(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:\TEST



3.在Servlet中取得路径:

(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值