读取资源文件的三种方法


web应用程序结构分析:
--src:基本存放.java和一些像struts.xml的文件。
--web-root:部署web项目就是部署这个文件。
--web-root下web-inf:存有页面(jsp/html)和.java生成的.class文件。
package
com.itheima.web.servlet; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.ResourceBundle; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //获取各种路径下的资源文件 public class ServletContextGetProperties extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据所学的由相对路径得到一个绝对路径 getRealPath() 只适合于web项目 //test1(); //2.ResourceBundle 资源文件加载器 它的适合范围 :web项目, java项目都可以 只要有src目录就可以了,它只能加载src下的资源 //test2(); //3.用类加载器ClassLoader 因为类加载器默认已经定位到classes目录 //3.1如何 得到类加载器
类加载器默认定位classes下:
ClassLoader cl = this.getClass().getClassLoader();//得到类加载器 //InputStream is = cl.getResourceAsStream("cfg.properties");//根据指定资源名称,得到一个文件流 src下cfg.properties //InputStream is = cl.getResourceAsStream("com/itheima/web/servlet/cfg.properties");//src下com.itheima.web.servlet 包下cfg.properties InputStream is = cl.getResourceAsStream("../cfg.properties");//WEB-INF下的cfg.properties ..代表返回上一级目录,相对于classes的上一级就是WEB-INF Properties props = new Properties();//得到一个Properties对象 props.load(is);//加载这个文件到内存 System.out.println(props.getProperty("p")); } private void test2() {
shift alt m 抽取方法
//2.ResourceBundle 资源文件加载器 它的适合范围 :web项目, java项目都可以 只要有src目录就可以了,它只能加载src下的资源 //ResourceBundle rb = ResourceBundle.getBundle("cfg");//基名 :就是不带 扩展名的文件名 cfg---------------获取src下的cfg.propertes ResourceBundle rb = ResourceBundle.getBundle("com.itheima.web.servlet.cfg");//src下的com.itheima.web.servlet包中cfg.properties文件 String value = rb.getString("p"); System.out.println(value); } private void test1() throws IOException, FileNotFoundException { //1.根据所学的由相对路径得到一个绝对路径 getRealPath() 只适合于web项目
web项目 的路径要在/web-inf/classes/后寻找
//String absolutePath = getServletContext().getRealPath("/WEB-INF/classes/cfg.properties");//代表查找src下的cfg.properties //String absolutePath = getServletContext().getRealPath("/WEB-INF/classes/com/itheima/web/servlet/cfg.properties");//代表查找src下com.itheima.web.servlet的cfg.properties String absolutePath = getServletContext().getRealPath("/WEB-INF/cfg.properties");//WEB-INF下的cfg.properties Properties props = new Properties();//得到一个Properties对象 props.load(new FileInputStream(absolutePath));//加载这个文件到内存 System.out.println(props.getProperty("p")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }


资源文件key=p value=src.package

 

转载于:https://www.cnblogs.com/baijin05/p/5069857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值