java 学习笔记 读取配置文件的三种方式

时间:2022-02-17 15:52:02
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);
} }