写路径
1.服务器与服务器
/:表示的是工程名
例如:request.getRequestDispatcher(“/index.jsp”)
表示:http://localhost:8080/工程名/index.jsp
2.服务器与浏览器
/:表示主机名
例如:response.sendRedirect(“/工程名/index.jsp”)
表示:http://localhost:8080/工程名/index.jsp
获取路径
方式一:
1.拿到类加载器
ClassLoader loader = BeanFactory.class.getClassLoader();
2.读文件
InputSream in = loader.getResourseAsStream(“bean.properties”);
3.前提:
bean.properties文件在src目录下
方式二:
1.通过getRealPath(“虚拟路径”)取得真实路径
2.前提:是在web工程中
方式三:(专门读取properties文件)
1.通过核心对象:ResourceBundle
ResourceBundle bundle = ResourceBundle.getBundle(“bean”);
2.注意,资源文件如果有包名,则需带包名,另外资源文件bean不能带properties后缀,否则bean会被解读为包名,properties被视为资源文件;
3.此时可以直接通过bundle对象来获取资源的内容
String value = bundle.getString(String key);
按键取值
4.前提:
该对象只能读,不能写
只能读.properties类型的文件
只能读类路径下的文件