java 学习笔记 读取配置文件的三种方式
2014-07-21 01:42
621 查看
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读取配置文件的三种方式
- java中读取properties配置文件中的内容的三种方式
- java学习笔记之配置文件.properties文件的读取与存储
- java 三种读取配置文件的方式
- web工程中jar读取 配置的文件,java工程中jar 读取配置的文件方式
- java反射学习笔记三(改进笔记二放到配置文件中)
- Java学习笔记——JDBC读取properties属性文件
- java读取.properties配置文件 的几种方式
- WorldWind Java 版学习:3、配置文件的读取
- 三种方式实现Flex读取XML配置文件
- 【Java学习笔记】基础知识学习17【文件文本读取写入】
- Java学习笔记之swing文件选取器 例子 (4种读写文件方式-和图片的读写方法)
- 读取Java文件到byte数组的三种方式
- java学习笔记6 - 使用commons configuration管理配置文件
- Java 学习笔记04:Spring XML配置文件Bean
- nginx 源码学习笔记(十三)——文件读写和配置文件读取
- nginx 源码学习笔记(十三)——文件读写和配置文件读取
- Smarty 学习笔记四 配置文件的读取
- java学习笔记_将中文字符导出txt文档_从txt文档读取文件