资源文件获取
资源文件可以看成是配置文件,一般的形式有两种:properties形式和XML形式 路径:一般是写在src目录下面,根目录
properties文件中数据的存储是以键值对的形式存在,每一行为一条数据,只能存储字符串形式的数据
Map==>Propertis
String key -- String value
注意:Properties文件中值的部分任意字符都会被当做值的一部分,尤其是空格
资源文件的作用:解决硬编码问题【代码逻辑中写死的代码】
方式一 ==》传统IO流方式:使用JDK中的Properties类进行数据读取
①:创建Properties对象
②:load加载资源文件流
③:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
方式二 ==》字节码对象获取流 注意:文件最终必须被编译到class文件存放的位置
①:创建Properties对象
②:通过字节码文件获得资源 (注意:字节码文件 加载流资源,前必须带/,若资源文件没有在包中,包的路径可以省略) 类名.class.getResourceAsStream(/包路径/文件名)
②:load加载资源文件流
③:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
方式三 ==》类加载器获取流 (采取通过线程获得类加载器)
①:创建Properties对象
②:通过当前类的字节码文件得到一个类加载器 类名.class.getClassLoader()
通过线程获得类加载器 Thread.currentThread().getContextClassLoader()
③:用类加载器加载流资源
④:load加载资源文件流
⑤:读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
注意:方式一流资源文件路径是写死的 存在隐编码问题 不推荐使用!
推荐使用方式三中的线程获取类加载器 读取配置文件!!!
---------------------
方法三代码:
public class AppConfig {
private static Map<String, String> property = new HashMap<>();
/**
* 获取资源文件信息并加入list中
*
* @return
* @throws IOException
*/
public static Map<String, String> getResource() {
// ①创建Properties对象:用于读取文件
Properties properties = new Properties();
// ②通过线程获得类加载器
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
// ③类加载器获得获得流资源
// 资源文件在src目录下面:直接写文件路径 不需要/ 如果写在其他的包路径==》包路径/子包路径/子包路径...../资源文件的名字
InputStream is = contextClassLoader.getResourceAsStream("application.properties");
//④properties加载流资源
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
// ⑤读取数据:getProperty(key):返回资源文件中对应key中的值(字符串),若key不存在则返回null
property.put("appName", properties.getProperty("appName"));
property.put("res_path", properties.getProperty("res_path"));
return property;
}
}
xml文件解析
方法一:dom4j
package com.wbyq.httpserver.config;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 读取XML。
* <servlet>
<url>/login.action</url>
<className>com.pro.web.LoginServlet</className>
</servlet>
* @author Administrator
*
*/
public class WebXMLConfig {
private static final List<ServletMapping> webxmls = new ArrayList<ServletMapping>( );
public static String findByURL(String url) {
for(ServletMapping sm : webxmls ) {
if(sm.getUrl().equals( url ) ) {
return sm.getClassName();
}
}
return null;
}
public static void init() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read("web.xml");
Element root = document.getRootElement();
List<Element> servlets = root.elements();
for(Element servlet : servlets) {
ServletMapping mapping = new ServletMapping();
mapping.setUrl( servlet.elementTextTrim("url") );
mapping.setClassName( servlet.elementTextTrim("className") );
webxmls.add( mapping );
}
}
static class ServletMapping{
private String url;
private String className;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "ServletMapping [url=" + url + ", className=" + className + "]";
}
}
}