`
snail_fish
  • 浏览: 32381 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java中获取资源文件

    博客分类:
  • Java
阅读更多

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

userName = snail
age = 24
password = 123456

然后我们再建立一个类Constans.java,附上静态变量

package testproperties;

public  class Constants {

	public static String userName;
	public static int age;
	public static String password; 
}

接下来的工作就尝试着如何获取properties文件类定义的姓名、年龄和密码了,新建一个InitProperties类

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics