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. php上传sae路径问题

    我们写php时,会有 define('POOT_PATH', dirname(__FILE__)); 但是sea中没有用,所以我们要用 define("ROOT_PATH", $_ ...

  2. iOS开发-- 利用AVPlayer播放远程音乐和视频

    一.简单的播放音乐和视频,播放视频的工具栏需要自己写 二.利用老师封装的框架实现视频播放 链接:http://pan.baidu.com/s/1hrEKlus 密码:8e7g

  3. Javaweb容器的四种作用域

    几乎所有web应用容器都提供了四种类似Map的结构:application session request page,Jsp或者Servlet通过向着这四个对象放入数据,从而实现Jsp和Servlet ...

  4. html5的一些表单属性。

    <pre>input属性:placeholder:输入框的默认值,向用户显示描述性说明文字或者提示信息 autocomplete:值为on和off..on则代表当该字段填写并提交后再返回该 ...

  5. 系统间通信(10)——RPC的基本概念

    1.概述 经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的讲解:RPC.在后续的几篇文章中,我们首先讲解R ...

  6. 【bzoj2809】 Apio2012—dispatching

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 (题目链接) 题意 给出一棵树,每个节点有两个权值${c}$,${L}$,分别代表花费和领导力 ...

  7. Ajax请求接口加密研究(针对网页前端的接口安全加密机制研究)

    通常我们在h5前端调用后台接口时,一般是ajax,那么接口的安全成了一个问题. 这里可以肯定的说,前端调用的接口一定要验证! 然后剖析了微信网页版.京东网页版这些,也都是通过接口的形势绑定数据,所以在 ...

  8. SVN分支研究

    在结合之前总结的定制开发的产品版本开发问题解决的方法:http://www.cnblogs.com/EasonJim/p/5971906.html,今天来研究以下用SVN处理这类的问题. 研究SVN分 ...

  9. ListView优化-ViewHolder的优化备份

    ViewHolder.java package cn.edu.bzu.util; import android.content.Context; import android.util.SparseA ...

  10. Bzoj1449 [JSOI2009]球队收益

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 741  Solved: 423 Description Input Output 一个整数表示联盟里所有球 ...