Web应用读资源文件
来源:互联网 发布:living earth mac 编辑:程序博客网 时间:2024/06/06 00:11
1.资源文件放在src下面,编译之后就放在web-inf/classes的目录下了,利用context读取:
public String test1() {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/pro.properties");
Properties pro = new Properties();
try {
pro.load(is);
String name = pro.getProperty("name");
return name;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
public String test2() {
try {
//获取文件路径
Stringpath = this.getServletContext().getRealPath("WEB-INF/classes/pro.properties");
//获取文件名
String filename = path.substring(path.lastIndexOf("\\"),path.length());
InputStream is = new FileInputStream(path);
Properties pro = new Properties();
pro.load(is);
String name = pro.getProperty("name");
return name+"-$-----"+filename;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
2. 资源文件放在src下面,普通java类读取文件
①static {
Properties pro = new Properties();
InputStream fin = null;
一、直接放在src下面这样读
fin= UtilsDb.class.getClassLoader().getResourceAsStream("dbinfo.properties");
二、和当前类放在同一包下,下面这样读
fin= UtilsDb.class.getResourceAsStream("dbinfo.properties");
try {
pro.load(fin);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
name = pro.getProperty("name");
password = pro.getProperty("password");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
- Web应用读资源文件
- web应用加载资源文件
- 加载web应用资源文件路径
- ServletContext读取web应用中的资源文件
- ServletContext 读取Web应用中的资源文件
- javaweb读取 web 应用中的资源文件
- 资源文件打包到Web自定义控件中应用
- 资源文件打包到Web自定义控件中应用
- WEB应用中普通Java程序如何读取资源文件
- java_web11-ServletContext读取Web应用中的资源文件
- WEB应用中读取资源文件的方式有三种
- ServletContext读取Web应用下的文件资源
- Servlet之ServletContext读取web应用中的资源文件
- 获取web应用下加载资源文件的方法
- web应用中普通java程序读取资源文件
- web应用中路径问题以及读取web应用下资源文件
- 应用资源文件
- web下加载资源文件
- Io学习
- OpenCV图像旋转
- Codeforces Round #138 (Div. 2)
- java.util.concurrent(JUC)的研究----概述
- Mac OS下给eclipse配置自动提示
- Web应用读资源文件
- 详有记录
- 【强连通分量】太空战队
- cdev结构 and MKDEV
- Eclipse 3.4上使用FlexBuilder
- TMS320F28027之CPU定时器
- VS2008下配置GSL
- php+jquery +ajax +json
- 设计模式-代理