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. 高手详解SQL性能优化十条经验

    1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...

  2. [转]Java_List元素的遍历和删除

    原文地址:http://blog.csdn.net/insistgogo/article/details/19619645 1.创建一个ArrayList List<Integer> li ...

  3. 【转】如何建立一个样式新颖的CSS3搜索框

    在线演示 搜索框大概是web开发中最常用的UI元素之一,我想基本没有必要去介绍如何使用它.无论是网站还是web应用,都会为了增强用户体验而添加它,那么你是不是也想过设计一个别致的搜索框? 在今天的文章 ...

  4. iOS开发小技巧--定时器的使用技巧

    一.定时器的使用技巧 -- 定义好了定时器后,添加两个方法,一个是添加定时器的方法,另一个是移除定时器的方法. 使用的时候也要注意,一定先移除之前的timer,然后再添加timer

  5. mybatis学习(一) mybatis框架的特性

    mybatis 的源代码地址是https://github.com/mybatis/mybatis-3/ 以及相关文档 All the information i get from http://ww ...

  6. FastFDFS_Jave客户端调用(亲测可用)

    一.配置文件(fdfs_client.properties) 1 2 3 4 5 6 7 8 9 10 connect_timeout = 30 network_timeout = 60 charse ...

  7. Entity Framework Code First (六)存储过程

    声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...

  8. tomcat提示警告: An attempt was made to authenticate the locked user"tomcat"

    启动tomcat7之后,运行正常,但是运行一段时间就会提示以下警告: 十二月 04, 2013 5:10:15 下午 org.apache.catalina.realm.LockOutRealm au ...

  9. bzoj 1588 splay模板题

    用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...

  10. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...