当前位置 > 主页 > 万和大讲堂 >


南京Java培训机构 J2SE API读取Properties文件的六种方法

2015-12-18 14:46

     使用J2SE API读取Properties文件的六种方法:

 

  1.使用java.util.Properties类的load()方法

 

  示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));

 

  Properties p = new Properties();

 

  p.load(in);南京Java培训机构

 

  2.使用java.util.ResourceBundle类的getBundle()方法

 

  示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

 

  3.使用java.util.PropertyResourceBundle类的构造函数

 

  示例: InputStream in = new BufferedInputStream(new FileInputStream(name));

 

  ResourceBundle rb = new PropertyResourceBundle(in);

 

  4.使用class变量的getResourceAsStream()方法

 

  示例: InputStream in = JProperties.class.getResourceAsStream(name);

 

  Properties p = new Properties();

 

  p.load(in);

 

  5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

 

  示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);

 

  Properties p = new Properties();

 

  p.load(in);南京Java培训机构

 

  6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

 

  示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);

 

  Properties p = new Properties();

 

  p.load(in);

 

  补充

 

  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

 

  示例:InputStream in = context.getResourceAsStream(path);

 

  Properties p = new Properties();

 

  p.load(in);

 

  完整的示例:

 

  JProperties.java文件

 

  package com.kindani;

 

  //import javax.servlet.ServletContext;

 

  import java.util.*;南京Java培训机构

 

  import java.io.InputStream;

 

  import java.io.IOException;

 

  import java.io.BufferedInputStream;

 

  import java.io.FileInputStream;

 

  /**

 

  * 使用J2SE API?取Properties文件的六种方法

 

  * User: SYNFORM

 

  * Date: 2005/07/12

 

  * Time: 18:40:55

 

  * To change this template use File | Settings | File Templates.

 

  */

 

  public class JProperties {

 

  public final static int BY_PROPERTIES = 1;

 

  public final static int BY_RESOURCEBUNDLE = 2;

 

  public final static int BY_PROPERTYRESOURCEBUNDLE = 3;

 

  public final static int BY_CLASS = 4;

 

  public final static int BY_CLASSLOADER = 5;

 

  public final static int BY_SYSTEM_CLASSLOADER = 6;

 

  public final static Properties loadProperties(

 

  final String name, final int type) throws IOException {

 

  Properties p = new Properties();

 

  InputStream in = null;

 

  if (type == BY_PROPERTIES) {

 

  in = new BufferedInputStream(new FileInputStream(name));

 

  assert (in != null);

 

  p.load(in);南京Java培训机构

 

  } else if (type == BY_RESOURCEBUNDLE) {

 

  ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

 

  assert (rb != null);

 

  p = new ResourceBundleAdapter(rb);

 

  } else if (type == BY_PROPERTYRESOURCEBUNDLE) {

 

  in = new BufferedInputStream(new FileInputStream(name));

 

  assert (in != null);

 

  ResourceBundle rb = new PropertyResourceBundle(in);

 

  p = new ResourceBundleAdapter(rb);

 

  } else if (type == BY_CLASS) {

 

  assert (JProperties.class.equals(new JProperties().getClass()));

 

  in = JProperties.class.getResourceAsStream(name);

 

  assert (in != null);

 

  p.load(in);

 

  //return new JProperties().getClass().getResourceAsStream(name);

 

  } else if (type == BY_CLASSLOADER) {

 

  assert (JProperties.class.getClassLoader().equals(

 

  new JProperties().getClass().getClassLoader()));

 

  in = JProperties.class.getClassLoader().getResourceAsStream(name);

 

  assert (in != null);

 

  p.load(in);南京Java培训机构

 

  //return new JProperties().getClass().

 

  getClassLoader().getResourceAsStream(name);

 

  } else if (type == BY_SYSTEM_CLASSLOADER) {

 

  in = ClassLoader.getSystemResourceAsStream(name);

 

  assert (in != null);

 

  p.load(in);

 

  }

 

  if (in != null) {

 

  in.close();

 

  }

 

  return p;

 

  }

 

  // ---------------------------------------------- servlet used

 

  /*

 

  public static Properties loadProperties(

 

  ServletContext context, String path) throws IOException {

 

  assert (context != null);

 

  InputStream in = context.getResourceAsStream(path);

 

  assert (in != null);

 

  Properties p = new Properties();

 

  p.load(in);

 

  in.close();

 

  re


最近开班 more>
  • Web前端开发
  • 软件测试
  • 软件测试预科班
  • AI大模型+全栈开发开班
  • 云原生精英班
  • 云网预科班
  • 开发课程基础班第三期
  • 开发课程基础班第二期
  • 开发课程基础班第五期
  • Java全栈
  • CISP
  • HCIP-cloud
  • HCIE-Datacom(HCIA,HCIP基础)
  • HCIP-Datacom(HCIA基础)
  • HCIA-Datacom(0基础)
  • HCIE-Datacom(HCIA,HCIP基础)
  • HCIP-Datacom(HCIA基础)
  • HCIA-Datacom(0基础)
  • OCP 19C
  • RHCA
  • 6月9日
  • 5月21日
  • 5月14日
  • 6月9日
  • 5月7日
  • 5月26日
  • 5月19日
  • 5月12日
  • 6月3日
  • 6月9日
  • 随时开课
  • 7月12日
  • 5月19日
  • 5月19日
  • 5月7日
  • 5月10日
  • 5月24日
  • 5月24日
  • 随时开课
  • 随时开课
    • 姓 名 :
    • 电 话 :
    • 课 程 :

技术交流群

  • Java大数据交流群560819979加入
  • Python技术交流群595083299加入
  • Oracle技术交流群595119011加入
  • Web前端技术交流群604697610加入
  • Huawei技术交流群482919361加入
  • Redhat技术交流群587875348加入
  • UI设计技术交流群511649801加入
  • Cisco技术交流群596886705加入
  • IT运维技术交流群605888381加入