javaweb读取配置文件的4种方法
来源:互联网 发布:大数据应用软件 编辑:程序博客网 时间:2024/06/02 02:07
方式一:采用ServletContext读取
获取配置文件的realpath,然后通过文件流读取出来或者通过方法getReasurceAsStream()。
因为是用ServletContext读取文件路径,所以配置文件可以放入在WEB-INF的classes目录中,也可以在应用层级及WEB-INF的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在WEB-INF及Web-Root下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
1.首先创建一个动态的javaweb项目,项目目录如下:
2.创建一个servlet(FileReader.java)
package com.xia.fileReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.MessageFormat;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FileReader extends HttpServlet {private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * response.setContentType("text/html;charset=UTF-8");目的是控制浏览器用UTF-8进行解码; * 这样就不会出现中文乱码了 */ response.setHeader("content-type","text/html;charset=UTF-8"); readSrcDirPropCfgFile(response);//读取src目录下的db1.properties配置文件 response.getWriter().println("<hr/>"); readWebRootDirPropCfgFile(response);//读取WebRoot目录下的db2.properties配置文件 response.getWriter().println("<hr/>"); readSrcSourcePackPropCfgFile(response);//读取src目录下的config目录中的db3.properties配置文件 response.getWriter().println("<hr/>"); readWEBINFPropCfgFile(response);//读取WEB-INF目录下的JDBC目录中的db4.properties配置文件}public void readSrcDirPropCfgFile(HttpServletResponse response) throws IOException {String path = "/WEB-INF/classes/db1.properties";InputStream in = this.getServletContext().getResourceAsStream(path); Properties props = new Properties(); props.load(in); String driver = props.getProperty("jdbc.driver"); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); response.getWriter().println("读取src目录下的db1.properties配置文件"); response.getWriter().println(MessageFormat.format( "driver={0},url={1},username={2},password={3}", driver,url, username, password));}public void readWebRootDirPropCfgFile(HttpServletResponse response) throws IOException{String path = "/db2.properties";InputStream in = this.getServletContext().getResourceAsStream(path); Properties props = new Properties(); props.load(in); String driver = props.getProperty("jdbc.driver"); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); response.getWriter().println("读取WebRoot目录下的db2.properties配置文件"); response.getWriter().println(MessageFormat.format( "driver={0},url={1},username={2},password={3}", driver,url, username, password));}public void readSrcSourcePackPropCfgFile(HttpServletResponse response) throws IOException {String path = "/WEB-INF/classes/config/db3.properties";String realPath = this.getServletContext().getRealPath(path); InputStreamReader reader = new InputStreamReader(new FileInputStream(realPath),"UTF-8"); Properties props = new Properties(); props.load(reader); String driver = props.getProperty("jdbc.driver"); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); response.getWriter().println("读取src目录下的config目录中的db3.properties配置文件"); response.getWriter().println(MessageFormat.format( "driver={0},url={1},username={2},password={3}", driver,url, username, password));}public void readWEBINFPropCfgFile(HttpServletResponse response) throws IOException {String path = "/WEB-INF/JDBC/db4.properties";String realPath = this.getServletContext().getRealPath(path); System.out.println("realPath:"+realPath); System.out.println("contextPath:"+this.getServletContext().getContextPath()); InputStreamReader reader = new InputStreamReader(new FileInputStream(realPath),"UTF-8"); Properties props = new Properties(); props.load(reader); String driver = props.getProperty("jdbc.driver"); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); response.getWriter().println("读取WEB-INF目录下的JDBC目录中的db4.properties配置文件"); response.getWriter().println(MessageFormat.format( "driver={0},url={1},username={2},password={3}", driver,url, username, password));}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}}
3.配置servlet(web.xml)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>javaReaderFile</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>FileReader</servlet-name> <servlet-class>com.xia.fileReader.FileReader</servlet-class> </servlet> <servlet-mapping> <servlet-name>FileReader</servlet-name> <url-pattern>/FileReader</url-pattern> </servlet-mapping></web-app>4.测试
方式二:采用ResourceBundle类读取配置信息
优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。
缺点:只能加载类src下面的资源文件且只能读取.properties文件。
方式三:采用ClassLoader方式进行读取配置信息
优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息
缺点:只能加载类src下面的资源文件,不适合装载大文件,否则会导致jvm内存溢出
package com.xia.fileReader;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties;public class ReadByClassLoader {public static void main(String[] args) throws IOException {readPropFileByClassLoad();} public static void readPropFileByClassLoad() throws IOException{ //读取src下面config包内的配置文件db3.properties InputStream in = ReadByClassLoader.class.getClassLoader().getResourceAsStream("config/db3.properties"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); Properties props = new Properties(); props.load(br); for(Object s: props.keySet()){ System.out.println(s+":"+props.getProperty(s.toString())); } }}
方式四: PropertiesLoaderUtils工具类
修改Properties
0 0
- javaweb读取配置文件的4种方法
- javaweb读取配置文件的4种方法
- JavaWeb读取配置文件路径的两种方式
- javaweb中读取配置文件的方式
- JavaWeb项目中读取配置文件的方式
- 读取配置文件的方法
- javaweb工程 配置文件读取
- Java/Java Web读取配置文件的4种方法
- 配置文件读取的n种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- xml文件(Activity_main)
- APUE——Chapter 5:标准I/O库
- deltav 安装过程
- Android屏幕适配全攻略(最权威的官方适配指导)
- app启动速度阶段指标
- javaweb读取配置文件的4种方法
- 21个实用的Javascript数据图表插件
- ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
- OpenStack Newton 新功能介绍
- 去掉前面的和中间的数字
- fileupload和cos文件上传技术区别
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
- 文章标题
- 利用PLSQL创建定时任务