Java/JavaWeb中读取资源文件
1、一般工程中使用I/O类指定文件的绝对路径读取
FileInputStream fis = new FileInputStream("src/main/resources/zsm.properties");
ppt.load(fis);
String memAddr1 = ppt.getProperty("memAddr1");
2、Web工程中可以使用ServletContext或ClassLoader来读取
2.1、通过ServletContext来读取资源文件,文件路径是相对于web项目(如/JspServletFeature)根路径而言的。
2.2、通过ClassLoader来读取,文件路径是相对于类目录而言的(maven工程中一般为/target/classes)
示例如下
(1)文件位置
放在src目录(或其子目录)下是相对于项目根目录如JspServletFeature的路径
放在JavaResources下是相对于类目录即classes的目录

(2)代码
// 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
out.println("\n使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature):");
readFileByServletContext(response, "FileReadFile1.properties");
readFileByServletContext(response, "/FileReadFile1.properties");
readFileByServletContext(response, "WEB-INF/classes/FileReadFile2.properties");
readFileByServletContext(response, "/WEB-INF/classes/FileReadFile2.properties");
readFileByServletContext(response, "WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
readFileByServletContext(response, "/WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
// 使用ClassLoader读取资源文件,相对于类目录(即classes)
out.println("\n使用ClassLoader读取资源文件,相对于类目录(即classes):");
readFileByClassLoader(response, "../../FileReadFile1.properties");
readFileByClassLoader(response, "/../../FileReadFile1.properties");
readFileByClassLoader(response, "FileReadFile2.properties");
readFileByClassLoader(response, "/FileReadFile2.properties");
readFileByClassLoader(response, "com/zsm/util/FileReadFile3.properties");
readFileByClassLoader(response, "/com/zsm/util/FileReadFile3.properties"); // 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
synchronized void readFileByServletContext(HttpServletResponse response, String filePath) throws IOException {
InputStream in = this.getServletContext().getResourceAsStream(filePath);
Properties prop = new Properties();
prop.load(in);
String fileName = prop.getProperty("fileName");
String name = prop.getProperty("name");
String company = prop.getProperty("company");
in.close();
response.getWriter().println(MessageFormat.format("filePath={0}, fileName={1}, name={2}, company={3}",
filePath, fileName, name, company));
} // 使用ClassLoader读取资源文件,相对于类目录(即classes)
synchronized void readFileByClassLoader(HttpServletResponse response, String filePath) throws IOException {
// 获取到装载当前类的类装载器
ClassLoader loader = FileReadServlet.class.getClassLoader();
InputStream in = loader.getResourceAsStream(filePath);
Properties prop = new Properties();
prop.load(in);
String fileName = prop.getProperty("fileName");
String name = prop.getProperty("name");
String company = prop.getProperty("company");
in.close();
response.getWriter().println(MessageFormat.format("filePath={0}, fileName={1}, name={2}, company={3}",
filePath, fileName, name, company));
}
(3)结果

Java/JavaWeb中读取资源文件的更多相关文章
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
- (转)java 从jar包中读取资源文件
(转)java 从jar包中读取资源文件 博客分类: java 源自:http://blog.csdn.net/b_h_l/article/details/7767829 在代码中读取一些资源文件 ...
- Java-Servlet--《12-WEB应用中的普通Java程序如何读取资源文件.mp4》 有疑问
\第五天-servlet开发和ServletConfig与ServletContext对象\12-WEB应用中的普通Java程序如何读取资源文件.mp4; 多层时,DAO为了得到资源文件中的配置参数: ...
- 深入jar包:从jar包中读取资源文件getResourceAsStream
一.背景 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等). 在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不 ...
- 【解惑】深入jar包:从jar包中读取资源文件
[解惑]深入jar包:从jar包中读取资源文件 http://hxraid.iteye.com/blog/483115 TransferData组件的spring配置文件路径:/D:/develop/ ...
- WEB应用中的普通Java程序如何读取资源文件
package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Serv ...
- [Java基础] 深入jar包:从jar包中读取资源文件
转载: http://hxraid.iteye.com/blog/483115?page=3#comments 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等).在单独运行的时候这些简单的 ...
- WEB应用中普通java代码如何读取资源文件
首先: 资源文件分两种:后缀.xml文件和.properties文件 .xml文件:当数据之间有联系时用.xml .properties文件:当数据之间没有联系时用.properties 正题: ...
- Java项目中读取properties文件,以及六种获取路径的方法
下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...
随机推荐
- js日期显示效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- SpringMVC与Struts2区别与比较总结
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...
- Centering, Scaling and Normalizing
Centering: 使数据的均值(Empirical Mean)变为0的过程. 具体操作:原始数据Xn(n=1, 2, 3, ..., n)减去均值. Scaling: 使数据的标准差(Empiri ...
- Lock读写锁技术的妙用
1.面试题1:三个线程读,三个线程写同一个数据 public class ReadWriteLockTest { public static void main(String[] args) { fi ...
- viewport ---移动端详解
转自---http://www.cnblogs.com/2050/p/3877280.html 移动前端开发之viewport的深入理解 在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上 ...
- XHTML与HTML的区别
XHTML:可扩展超文本标记语言,是一种置标语言,表现方式与超文本标记语言(HTML)类似,不过语法上更加严格. 参考:http://baike.baidu.com/search/word?word= ...
- 禁止apache显示目录索引的常见方法(apache禁止列目录)
禁止Apache显示目录索引,禁止Apache显示目录结构列表,禁止Apache浏览目录,这是网上提问比较多的,其实都是一个意思.下面说下禁止禁止Apache显示目录索引的常见的3种方法. 要实现禁止 ...
- Search and Replace的正则表达式
以下例子均在Search and Replace中适用,EditPlus也有正则表达式,但有点不一样. 1.1 基本操作符 * 重复0次或更多次 + 重复一次或更多次 ? 重复一次 | 或 操作符 ! ...
- hbase批量数据导入报错:NotServingRegionException
批量导入数据到hbase的时候,报错: org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException: Failed 1 ac ...
- 关于gcd的几个问题
这两天刷了几个关于gcd的很类似的问题,总结一下: BZOJ2818 1<=x<=n,1<=y<=n,求满足gcd(x,y)=质数的个数 BZOJ2190 1<=x< ...