1、一般工程中使用I/O类指定文件的绝对路径读取

  1. FileInputStream fis = new FileInputStream("src/main/resources/zsm.properties");
  2. ppt.load(fis);
  3. 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)代码

  1. // 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
  2. out.println("\n使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature):");
  3. readFileByServletContext(response, "FileReadFile1.properties");
  4. readFileByServletContext(response, "/FileReadFile1.properties");
  5. readFileByServletContext(response, "WEB-INF/classes/FileReadFile2.properties");
  6. readFileByServletContext(response, "/WEB-INF/classes/FileReadFile2.properties");
  7. readFileByServletContext(response, "WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
  8. readFileByServletContext(response, "/WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
  9. // 使用ClassLoader读取资源文件,相对于类目录(即classes)
  10. out.println("\n使用ClassLoader读取资源文件,相对于类目录(即classes):");
  11. readFileByClassLoader(response, "../../FileReadFile1.properties");
  12. readFileByClassLoader(response, "/../../FileReadFile1.properties");
  13. readFileByClassLoader(response, "FileReadFile2.properties");
  14. readFileByClassLoader(response, "/FileReadFile2.properties");
  15. readFileByClassLoader(response, "com/zsm/util/FileReadFile3.properties");
  16. readFileByClassLoader(response, "/com/zsm/util/FileReadFile3.properties");
  17.  
  18. // 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
  19. synchronized void readFileByServletContext(HttpServletResponse response, String filePath) throws IOException {
  20. InputStream in = this.getServletContext().getResourceAsStream(filePath);
  21. Properties prop = new Properties();
  22. prop.load(in);
  23. String fileName = prop.getProperty("fileName");
  24. String name = prop.getProperty("name");
  25. String company = prop.getProperty("company");
  26. in.close();
  27. response.getWriter().println(MessageFormat.format("filePath={0}, fileName={1}, name={2}, company={3}",
  28. filePath, fileName, name, company));
  29. }
  30.  
  31. // 使用ClassLoader读取资源文件,相对于类目录(即classes)
  32. synchronized void readFileByClassLoader(HttpServletResponse response, String filePath) throws IOException {
  33. // 获取到装载当前类的类装载器
  34. ClassLoader loader = FileReadServlet.class.getClassLoader();
  35. InputStream in = loader.getResourceAsStream(filePath);
  36. Properties prop = new Properties();
  37. prop.load(in);
  38. String fileName = prop.getProperty("fileName");
  39. String name = prop.getProperty("name");
  40. String company = prop.getProperty("company");
  41. in.close();
  42. response.getWriter().println(MessageFormat.format("filePath={0}, fileName={1}, name={2}, company={3}",
  43. filePath, fileName, name, company));
  44. }

(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. [转]servlet中的service, doGet, doPost方法的区别和联系

    原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...

  2. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  3. jquery 双击修改某项值

    双击修改某项值 $(function() { $('td.breakword').dblclick(function(){ $(this).addClass('input').html('<in ...

  4. SQLite数据库的基本操作

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...

  5. Hive 的分桶 & Parquet 概念

    分区 & 分桶 都是把数据划分成块.分区是粗粒度的划分,桶是细粒度的划分,这样做为了可以让查询发生在小范围的数据上以提高效率. 分区之后,分区列都成了文件目录,从而查询时定位到文件目录,子数据 ...

  6. phpcms /api/phpsso.php SQL Injection Vul

    catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link:2. 漏洞触发条件3. 漏洞影响范围4. ...

  7. Automated CMS category, version identification (CMS vulnerability detection)

    catalog . 引言 . 不同CMS版本标的文件路径调研 . Code Example 1. 引言 微软解决大量CVE补丁更新的检测时候,采取的思路不是根据MD5对单个漏洞文件(.dll..sys ...

  8. git本地分支

    1. 新建并切换到该分支 $ git checkout -b iss53 Switched to a new branch 'iss53' 相当于: $ git branch iss53$ git c ...

  9. PHP的单引号与双引号的区别

    字符串应用 单引号 较好! 在某些特定情况下,单引号的效率比双引号高. PHP把单引号中的数据视为普通字符串,不再处理. 而双引号还要对其中的字符串进行处理,比如遇到$了会把其后的内容视为变量等.

  10. 软件工程个人作业 - week1

    <构建之法>阅读疑惑: 如何寻找开发效率和性能的均衡点?显然开发效率强调封装,重视代码重用.但是遗憾的是代码重用往往泛化了数据特征,降低了效率. 如何“公平”分配工作?尤其是团队中人员参差 ...