• 读取资源文件的三种方法



    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
  • 相关阅读:
    安装MeeGo
    一、Android uevent架构
    android键盘映射(转)
    【转】密码学 :CSP的概念
    【原创翻译】链接DLL至可执行文件翻译自MSDN
    【转】c语言中的定义和声明
    Windows:32位程序运行在64位系统上注册表会重定向
    Wow64
    VS2008 ActiveX(ocx控件)的调试工具ActiveX Control Test Container安装说明
    小写bool和大写BOOL的区别
  • 原文地址:https://www.cnblogs.com/baijin05/p/5069857.html
Copyright © 2020-2023  润新知