<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天在写读取配置文件的方法,在网上找了好多种,分别整理出来,以后备用。</span>
1、java.util.ResourceBundle类的getBundle()方法
ResourceBundle bundle = ResourceBundle.getBundle(文件名,Locale.CHINA);
//文件名需要去掉后缀
String s = bundle.getString("URL");
System.out.println(s);
2、 java.util.PropertyResourceBundle类
- InputStream in = new BufferedInputStream(new FileInputStream(name));
- ResourceBundle rb = new PropertyResourceBundle(in);
3、java.lang.ClassLoader的getResourceAsStream()
- InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
4、java.lang.ClassLoader类的getSystemResourceAsStream()
- InputStream in = ClassLoader.getSystemResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
5、javax.servlet.ServletContext的getResourceAsStream()
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
6、apache的类库中的PropertiesConfiguration 类
PropertiesConfiguration config = new PropertiesConfiguration("file path");
config.getString("your prop name");
7、java.util.Properties类的load()
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
8、class变量的getResourceAsStream()
- InputStream in = 类名.class.getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);