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中读取资源文件的更多相关文章

  1. java 从jar包中读取资源文件

    在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...

  2. (转)java 从jar包中读取资源文件

    (转)java 从jar包中读取资源文件 博客分类: java   源自:http://blog.csdn.net/b_h_l/article/details/7767829 在代码中读取一些资源文件 ...

  3. Java-Servlet--《12-WEB应用中的普通Java程序如何读取资源文件.mp4》 有疑问

    \第五天-servlet开发和ServletConfig与ServletContext对象\12-WEB应用中的普通Java程序如何读取资源文件.mp4; 多层时,DAO为了得到资源文件中的配置参数: ...

  4. 深入jar包:从jar包中读取资源文件getResourceAsStream

    一.背景 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等). 在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不 ...

  5. 【解惑】深入jar包:从jar包中读取资源文件

    [解惑]深入jar包:从jar包中读取资源文件 http://hxraid.iteye.com/blog/483115 TransferData组件的spring配置文件路径:/D:/develop/ ...

  6. WEB应用中的普通Java程序如何读取资源文件

    package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Serv ...

  7. [Java基础] 深入jar包:从jar包中读取资源文件

    转载: http://hxraid.iteye.com/blog/483115?page=3#comments 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等).在单独运行的时候这些简单的 ...

  8. WEB应用中普通java代码如何读取资源文件

    首先: 资源文件分两种:后缀.xml文件和.properties文件 .xml文件:当数据之间有联系时用.xml .properties文件:当数据之间没有联系时用.properties 正题:   ...

  9. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

随机推荐

  1. Excel For Java

    Excel 需要了解的东西 首先是Excel是一个文件 - File,工作簿 - workbook,sheet - sheet,单元格 - cell. 然后就根据Excel是一个什么东西来创建一个Ex ...

  2. vmware 在NAT模式下连接上外网

    文章: http://www.2cto.com/os/201504/389011.html

  3. PHP ElasticSearch的使用

    系统是Windows server 2003. ElasticSearch是一个基于Lucene的稳定的.分布式.RESTFul的搜索引擎.其实所谓的RestFul就是它提供URL供你调用(建立索引和 ...

  4. git使用相关文章的链接

    http://search.fishc.com/cse/search?s=14988791857133860392&q=git&partner=discuz http://bbs.fi ...

  5. 【CodeForces 698A】Vacations

    f[i][0..2]表示第i天休息|运动|比赛最少的休息天数. #include <cstdio> #include <cstring> #include <algori ...

  6. OO(Object Oriented)思想和PO(Procedure-Oriented)思想

    对象将需求用类一个个隔开,就象用储物箱把东西一个个封装起来一样,需求变了,分几种情况,最严重的是大变,那么每个储物箱都要打开改,这种方法就不见得有好处:但是这种情况发生概率比较小,大部分需求变化都是局 ...

  7. c# 百度api语音识别

    public static string getStrAccess(string para_API_key, string para_API_secret_key) { //方法参数说明: //par ...

  8. 配置服务器有错/usr/libexec/gconf-sanity-check-2的退出状态为256

    问题描述: CentOS启动的时候报如下错误:“配置服务器有错/usr/libexec/gconf-sanity-check-2的退出状态为256” 问题原因: 在装hadoop的时候误删了/tmp文 ...

  9. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  10. 对iOS中Delegate的理解

    首先 协议protocol 和委托delegate 是两个完全不同的概念  放在一起说 是因为我们总是在同一个头文件里看到它们: 首先解释一下什么是委托 :举个例子 ,我工作的时候给你打电话,让你帮我 ...