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 学习笔记 读取配置文件的三种方式的更多相关文章

  1. Java学习笔记——显示当前日期的三种方式

    一.Date类:这是一种过时的表达方式 import java.util.Date; Date date = new Date(); System.out.println((1900+date.get ...

  2. Servlet读取配置文件的三种方式

    一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...

  3. Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!

    在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...

  4. Java反射获取class对象的三种方式,反射创建对象的两种方式

    Java反射获取class对象的三种方式,反射创建对象的两种方式 1.获取Class对象 在 Java API 中,提供了获取 Class 类对象的三种方法: 第一种,使用 Class.forName ...

  5. java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService

    前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...

  6. java加载配置文件的三种方式

    比如我们要加载db.properties文件 如图: 比如我们要加载source目录下的db.properties文件.就有以下几种方式 第一种是文件io流: public static void l ...

  7. 【java多线程】多线程的创建三种方式--笔记

    申明:线程的概念以及进程的相关概念,可以参考网络上其他资料,这里只讨论多线程是怎么实现. 一.多线程的简单理解 明白什么是多线程,小生通俗一点的理解为:在一个程序里,我想同时让这个程序完成多个任务. ...

  8. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  9. Java中 实现多线程成的三种方式(继承,实现,匿名内部类)

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. ButterKnife的安装与使用以及ButterKnife右键不显示的大坑

    作为从安卓的的入门选手,第一次看到还以为是个第三方呢,从github下来之后感觉不对啊,这么多东西,后来一搜原来是个插件,而且不用从github上下载. 安装的方法很简单. 第一步:打开安卓studi ...

  2. poj 3013 SPFA

    首先看题看的很懵.. 然后这题直接没想用Djstra做 TLE了.看discuss,Dijstra要用堆优化,也可以用SPFA做. 这里在网上找了这两种做法的区别,点多稠密图用Dij,以为它是操作点的 ...

  3. 文件系统的几种类型:ext3, swap, RAID, LVM

    分类: 架构设计与优化 1.  ext3 在异常断电或系统崩溃(不洁关机, unclean system shutdown  ).每个已挂载ext2文件系统计算机必须使用e2fsck程序来检查其一致性 ...

  4. 转:java获得当前文件路径

    第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...

  5. SNS团队Beta阶段第二次站立会议(2017.05.23)

    1.立会照片 2.每个人的工作 每个成员的分工: 成员 今天已完成的工作 明天计划完成的工作 罗于婕 完善代码规范文档 辅助完善生词本 龚晓婷 界面优化  辅助开发新功能 林仕庄 界面图标不对齐bug ...

  6. Swing-setBorder()用法-入门

    注:本文内容转自:Swing编程边框(Border)的用法总结.内容根据笔者理解稍有整理. 函数说明: public void setBorder(Border border) 设置此组件的边框.Bo ...

  7. 201521044091 《Java程序设计》第3周学习总结

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 本周学习总结 ...

  8. Servlet的生命周期与运行原理

    Servlet的生命周期:    1 加载classLoader    2 实例化 new    3 初始化 init(ServletConfig)    4 处理请求 service doGet d ...

  9. 201521123111《Java程序设计》第10周学习总结

    1. 本章学习总结 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容 线程不是程序 线程必须在程序对应的进程中运行 一个进程中可以同时执行多个线程,这些线程可以完成不同的功能. 2. 书面 ...

  10. 【SQL】- 基础知识梳理(三) - SQL连接查询

    一.引言 有时为了得到一张报表的完整数据,需要从两个或更多的表中获取结果,这时就用到了"连接查询". 二.连接查询 连接查询的定义: 数据库中的表通过键将彼此联系起来,从而获取这些 ...