java 学习笔记 读取配置文件的三种方式
package com.itheima.servlet.cfg; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoaderCfgServletDemo1 extends HttpServlet { //第一种方式,可以加载所有目录下的内容,但只用于web项目
//test11();
//test12();//ppp
//test13();
//第二种方式 ResouceBundle专门用于读取properties文件的,
//只用于加载类路径classes目录下的文件
//java项目和web项目都可以用
//test21();
//test22();//基名 ppp //第三种方式 就是用ClassLoader类加载器
//得到类加载器的方法 LoaderCfgServletDemo1.class.getClassLoader()
//类加载器一上来定位的目录是classes
//test31();
//test32();
//test33();
//曾经的回顾
//test34(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//test11();
//test12();
//test13();
//test21();
//test22();
//test31();
//test32();
//test33();
//test34();
}
//pp
public void test34() throws IOException, FileNotFoundException {
ClassLoader cl = LoaderCfgServletDemo1.class.getClassLoader();
URL url = cl.getResource("cfg.properties");//协议+主机名(包含端口)+资源地址
//System.out.println(url.getPath()); Properties p = new Properties();
p.load(new FileInputStream(url.getPath()));
System.out.println(p.getProperty("p"));
}
//pppp
public void test33() throws IOException, FileNotFoundException {
ClassLoader cl = LoaderCfgServletDemo1.class.getClassLoader();
//这是相对于classes这个目录而言,找它的上一级
InputStream is = cl.getResourceAsStream("../cfg.properties");
Properties p = new Properties();
p.load(is);
System.out.println(p.getProperty("p"));
}
//pp
public void test32() throws IOException, FileNotFoundException {
ClassLoader cl = LoaderCfgServletDemo1.class.getClassLoader();
InputStream is = cl.getResourceAsStream("cfg.properties");//如何写路径
Properties p = new Properties();
p.load(is);
System.out.println(p.getProperty("p"));
}
//ppp
public void test31() throws IOException, FileNotFoundException {
ClassLoader cl = LoaderCfgServletDemo1.class.getClassLoader();
InputStream is = cl.getResourceAsStream("com/itheima/servlet/cfg/cfg.properties");//如何写路径
Properties p = new Properties();
p.load(is);
System.out.println(p.getProperty("p"));
}
//ppp
public void test22() throws IOException, FileNotFoundException {
ResourceBundle rb = ResourceBundle.getBundle("com.itheima.servlet.cfg.cfg");//基名(包名.文件名(不带扩展名))
System.out.println(rb.getString("p"));
}
//pp
public void test21() throws IOException, FileNotFoundException {
ResourceBundle rb = ResourceBundle.getBundle("cfg");//基名
System.out.println(rb.getString("p"));
}
public void test13() throws IOException, FileNotFoundException {
//1第一种 ,用/代表当前应用 pppp
String path =getServletContext().getRealPath("/WEB-INF/cfg.properties");
//System.out.println(path);
Properties p = new Properties();
p.load(new FileInputStream(path)); System.out.println(p.getProperty("p"));
}
public void test12() throws IOException, FileNotFoundException {
//1第一种 ,用/代表当前应用 ppp
String path =getServletContext().getRealPath("/WEB-INF/classes/com/itheima/servlet/cfg/cfg.properties");
//System.out.println(path);
Properties p = new Properties();
p.load(new FileInputStream(path)); System.out.println(p.getProperty("p"));
}
private void test11() throws IOException, FileNotFoundException {
//1第一种 ,用/代表当前应用 pp
String path =getServletContext().getRealPath("/WEB-INF/classes/cfg.properties");
//System.out.println(path);
Properties p = new Properties();
p.load(new FileInputStream(path));
System.out.println(p.getProperty("p"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
} }
java 学习笔记 读取配置文件的三种方式的更多相关文章
- Java学习笔记——显示当前日期的三种方式
一.Date类:这是一种过时的表达方式 import java.util.Date; Date date = new Date(); System.out.println((1900+date.get ...
- Servlet读取配置文件的三种方式
一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...
- Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...
- Java反射获取class对象的三种方式,反射创建对象的两种方式
Java反射获取class对象的三种方式,反射创建对象的两种方式 1.获取Class对象 在 Java API 中,提供了获取 Class 类对象的三种方法: 第一种,使用 Class.forName ...
- java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- java加载配置文件的三种方式
比如我们要加载db.properties文件 如图: 比如我们要加载source目录下的db.properties文件.就有以下几种方式 第一种是文件io流: public static void l ...
- 【java多线程】多线程的创建三种方式--笔记
申明:线程的概念以及进程的相关概念,可以参考网络上其他资料,这里只讨论多线程是怎么实现. 一.多线程的简单理解 明白什么是多线程,小生通俗一点的理解为:在一个程序里,我想同时让这个程序完成多个任务. ...
- Spring学习之实例化bean的三种方式
实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...
- Java中 实现多线程成的三种方式(继承,实现,匿名内部类)
---------------------------------------------------------------------------------------------------- ...
随机推荐
- 第1阶段——uboot分析之硬件初始化start.S(4)
分析uboot第一个执行函数_start(cpu/arm920t/start.S) 打开cpu/arm920t/start.S .globl _start // .globl定义一个全局符号" ...
- Linux-chmod命令(4)
chmod:(change mode)改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限. 格式 : [-cfvR][[+-=][rwxX]...][,...] 参数 1: -c ...
- asp.net mvc 4 项目升级到 asp.net mvc5
一.开始 1.打开或新建asp.net mvc 4项目 2.修改 global.asax文件 原: WebApiConfig.Register(GlobalConfiguration.Configur ...
- 【C++小白成长撸】--N阶幻方(魔阵)矩阵
解决方法:1.第一个元素放在第一行中间一列 2.下一个元素存放在当前元素的上一行.下一列. 3.如果上一行.下一列已经有内容,则下一个元素的存放位置为当前列的下一行. 在找上一行.下一行或者下一列的时 ...
- 如何让eclipse在程序修改后,点击运行可以自动保存。
preferences>run/debug>launching里面save required dirty editors before launching选always就自动保存咯选pro ...
- 201521123005《java程序设计》第三周学习总结
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.使用思维导图将这些碎片化的概念.知识组织起来.使用工具画出本周学习到的知识点. 参考资料: 百度脑图 XMind 2. 书面作业 ·Q ...
- Java课程设计-学生基本信息管理 201521123036
团队课程设计博客链接 团队博客链接 个人负责模块或任务说明 个人负责模块 任务说明 用户登录,注册 登录,注册,判断用户是否存在,添加用户 学生信息管理菜单 按钮,跳转相应界面,退出程序 学生信息添加 ...
- SSH复用代码最终版
web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...
- Hibernate第七篇【对象状态、一级缓存】
前言 本博文主要讲解Hibernate的细节-->对象的状态和一级缓存- 对象状态 Hibernate中对象的状态: - 临时/瞬时状态 - 持久化状态 - 游离状态 学习Hibernate的对 ...
- java.sql.Exception:setString 只能处理少于 32766 个字符的字符串
java.sql.Exception:setString 只能处理少于 32766 个字符的字符串 解决方式是 : 升级ojdbc的版本, 将原来的 ojdbc14_10.2.0.2.0.jar ...