Java/JavaWeb中读取资源文件

时间:2022-09-18 16:37:10

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的目录

  Java/JavaWeb中读取资源文件

(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/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. android studio导入 so ,jar 文件。

    环境为: Android Studio 1.0.2 如果是jar文件的话,请直接拷贝jar文件到项目的libs文件夹下,然后运行:Sync Project with Gradle Files.如下图2 ...

  2. BZOJ 2038 小Z的袜子(hose)(分组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2038 题意:给出n个袜子.m个询问,每个询问一个区间[L,R],询问这个区间中任意拿出两 ...

  3. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  4. linux_环境变量设置 utf-8

    echo $LANG 显示编码  :  en_US.UTF-8  英文urf8有时显示程序输出是? 解决方法: vim ~/.bashrc 最后一行追加: export LANG=zh_CN.UTF- ...

  5. CentOS下软件安装与卸载常用命令总结

    最近在折腾CentOS 7操作系统,主要是下载安装文件以及解决各项依赖问题,现对此过程中用到的有效的CentOS命令进行汇总总结. 1. 安装与卸载软件:yum.rpm.wget命令 首先,在Cent ...

  6. django 通过ajax完成登录

    一. 在users/views.py中加代码: from django.http import HttpResponse class LoginView(View): ""&quo ...

  7. Spring Cloud :断路器集群监控(Turbine)

    一. 简介      上一篇文章我们已经实现了对单个服务实例的监控,当然在实际应用中,单个实例的监控数据没有多大的价值,我们更需要的是一个集群系统的监控信息,这时我们就需要引入Turbine.Turb ...

  8. 《Gradle权威指南》--Groovy基础

    No1: Groovy中分号不是必须的 No2: Groovy中,单引号和双引号都可以定义一个字符串常量,不同的是单引号标记的是纯粹的字符串常量,而不是对字符串里的表达式做运算,但是双引号可以. ta ...

  9. poj2115 C Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29262   Accepted: 8441 Descr ...

  10. Win8系统108个运行命令 你能记住多少?

    Win8运行命令:程序和功能   取消了开始菜单的Win8让人感觉很不习惯,这才发现原来开始菜单可以做这么多事.不过Win8中的一些快捷键还沿用了Windows一直以来的习惯,比如按下Windows ...