使用J2SE API读取Properties文件的六种方法:
  1.使用Java.util.Properties类的load()方法
  示例:

      InputStream in = lnew BufferedInputStream(new FileInputStream(name));
  Properties p = new Properties();
  p.load(in);
  2.使用java.util.ResourceBundle类的getBundle()方法

示例: ResourceBundle rb = Bundle(name, Default());
  3.使用java.util.PropertyResourceBundle类的构造函数

 示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
  ResourceBundle rb = new PropertyResourceBundle(in);
  4.使用class变量的getResourceAsStream()方法

示例: InputStream in = ResourceAsStream(name);
  Properties p = new Properties();
  p.load(in);
  5.使用ClassLoader()所得到的java.lang.ClassLoadergetResourceAsStream()方法

 示例: InputStream in = ClassLoader().getResourceAsStream(name);
  Properties p = new Properties();
  p.load(in);
  6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

示例: InputStream in = SystemResourceAsStream(name);
  Properties p = new Properties();
  p.load(in);
  补充
  Servlet中可以使用javax.servlet.ServletContextgetResourceAsStream()方法

示例:InputStream in = ResourceAsStream(path);
  Properties p = new Properties();
  p.load(in);
    完整的示例:

JProperties.Java文件
  package com.kindani;
  //import javax.Servlet.ServletContext;
  import java.util.*;
  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
富士康 model b  * 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);
  } else if (type == BY_RESOURCEBUNDLE) {
  ResourceBundle rb = Bundle(name, Default());
  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 = ResourceAsStream(name);
  assert (in != null);
  p.load(in);
  //return new JProperties().getClass().getResourceAsStream(name);
  } else if (type == BY_CLASSLOADER) {
  assert (ClassLoader().equals(
  new JProperties().getClass().getClassLoader()));
  in = ClassLoader().getResourceAsStream(name);
  assert (in != null);
  p.load(in);
  //return new JProperties().getClass().
  getClassLoader().getResourceAsStream(name);
  } else if (type == BY_SYSTEM_CLASSLOADER) {
  in = SystemResourceAsStream(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 = ResourceAsStream(path);
  assert (in != null);
  Properties p = new Properties();
  p.load(in);
  in.close();
  return p;
  }
  */
  // ---------------------------------------------- support class
  /**
  * ResourceBundle Adapter class.
  */
  public static class ResourceBundleAdapter extends Properties {
  public ResourceBundleAdapter(ResourceBundle rb) {
  assert (rb instanceof java.util.PropertyResourceBundle);
  this.rb = rb;
  java.util.Enumeration e = rb.getKeys();
  while (e.hasMoreElements()) {
  Object o = e.nextElement();
  this.put(o, rb.getObject((String) o));
  }
  }
  private ResourceBundle rb = null;
  public ResourceBundle getBundle(String baseName) {
  Bundle(baseName);
  }
  public ResourceBundle getBundle(String baseName, Locale locale) {
  Bundle(baseName, locale);
oracle分页关键词  }
  public ResourceBundle getBundle(String baseName,
  Locale locale, ClassLoader loader) {
  Bundle(baseName, locale, loader);
  }
  public Enumeration getKeys() {
properties文件用什么打开  Keys();
  }
  public Locale getLocale() {
  Locale();
  }
  public Object getObject(String key) {
  Object(key);
  }
  public String getString(String key) {
  String(key);
  }
  public String[] getStringArray(String key) {
  StringArray(key);
  }
  protected Object handleGetObject(String key) {
  return ((PropertyResourceBundle) rb).handleGetObject(key);
  }
  }
  }
  JPropertiesTest.Java文件package st;
江苏计算机二级c语言  import junit.Framework.*;
  import com.kindani.JProperties;
  //import javax.Servlet.ServletContext;
  import java.util.Properties;
  public class JPropertiesTest extends TestCase {
  JProperties jProperties;
  String key = "helloworld.title";
  String value = "Hello World!";
  public void testLoadProperties() throws Exception {
  String name = null;
  Properties p = new Properties();
  name = "C:IDEAPProperties4Methodssrccom
  kindanitestLocalStrings.properties";
  p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES);
  assertEquals(value, p.getProperty(key));
  name = "st.LocalStrings";
  p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);
  assertEquals(value, p.getProperty(key));
  c语言指针查询assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));
  name = "C:IDEAPProperties4Methodssrccom
  kindanitestLocalStrings.properties";
  php可以干什么p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);
  assertEquals(value, p.getProperty(key));
  assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));
  name = "comkindanitestLocalStrings.properties";
  p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER);
  assertEquals(value, p.getProperty(key));
  name = "comkindanitestLocalStrings.properties";
  p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);
  assertEquals(value, p.getProperty(key));
  name = "testLocalStrings.properties";
  p = JProperties.loadProperties(name, JProperties.BY_CLASS);
  assertEquals(value, p.getProperty(key));
  }
  /*
  public void testLoadProperties2() throws Exception {
  ServletContext context = null;
  String path = null;
  Properties p = null;
  path = "/Web-INF/classes/LocalStrings.properties";
  p = JProperties.loadProperties(context, path);
  assertEquals(value, p.getProperty(key));
  }
  */
  }
   properties文件与JPropertiesTest.Java文件相同的目录下
  LocalStrings.properties文件

 # $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $
  # Default localized resources for example Servlets
  # This locale is en_US
  helloworld.title=Hello World!
  requestinfo.title=Request Information Example
  hod=Method:
  questuri=Request URI:
  requestinfo.label.protocol=Protocol:
  requestinfo.label.pathinfo=Path Info:
  addr=Remote Address:
  requestheader.title=Request Header Example
  requestparams.title=Request Parameters Example
  requestparams.params-in-req=Parameters in this request:
  -params=No Parameters, Please enter some
  requestparams.firstname=First Name:
  requestparams.lastname=Last Name:
  cookies.title=Cookies Example
  kies=Your browser is sending the following cookies:
  -cookies=Your browser isn't sending any cookies
  cookies.make-cookie=Create a cookie to send to your browser
  cookies.name=Name:
  cookies.value=Value:
  cookies.set=You just sent the following cookie to your browser:
  sessions.title=Sessions Example
  sessions.id=Session ID:
  ated=Created:
  sessions.lastAccessed=Last Accessed:
  sessions.data=The following data is in your session:
  sessions.adddata=Add data to your session
  sessions.dataname=Name of Session Attribute:
  sessions.datavalue=Value of Session Attribute


版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。