熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

使用J2SE API讀取Properties文件的六種方法

2022-06-13   來源: JSP教程 

  使用JSE API讀取Properties文件的六種方法
  
  使用javautilProperties類的load()方法
  
  示例 InputStream in = lnew BufferedInputStream(new FileInputStream(name));
  Properties p = new Properties();
  pload(in);
  
  使用javautilResourceBundle類的getBundle()方法
  
  示例 ResourceBundle rb = ResourceBundlegetBundle(name LocalegetDefault());
  
  使用javautilPropertyResourceBundle類的構造函數
  
  示例 InputStream in = new BufferedInputStream(new FileInputStream(name));
  ResourceBundle rb = new PropertyResourceBundle(in);
  
  使用class變量的getResourceAsStream()方法
  
  示例 InputStream in = JPropertiesclassgetResourceAsStream(name);
  Properties p = new Properties();
  pload(in);
  
  使用classgetClassLoader()所得到的javalangClassLoader的getResourceAsStream()方法
  
  示例 InputStream in = JPropertiesclassgetClassLoader()getResourceAsStream(name);
  Properties p = new Properties();
  pload(in);
  
  使用javalangClassLoader類的getSystemResourceAsStream()靜態方法
  
  示例 InputStream in = ClassLoadergetSystemResourceAsStream(name);
  Properties p = new Properties();
  pload(in);
  
  補充
  
  Servlet中可以使用javaxservletServletContext的getResourceAsStream()方法
  
  示例InputStream in = contextgetResourceAsStream(path);
  Properties p = new Properties();
  pload(in);
  
  完整的示例可以參考附件文件
  
  如何上傳文件誰知道請告訴以下 只好把source都貼上來了
  
  JPropertiesjava文件
  
  /**
  ** This program is free software
  **
  ** You may redistribute it and/or modify it under the terms of the GNU
  ** General Public License as published by the Free Software Foundation
  ** Version of the license should be included with this distribution in
  ** the file LICENSE as well as l If the license is not
  ** included with this distribution you may find a copy at the FSF web
  ** site at or or you may write to the
  ** Free Software Foundation Mass Ave Cambridge MA USA
  **
  ** THIS SOFTWARE IS PROVIDED ASIS WITHOUT WARRANTY OF ANY KIND
  ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY THE AUTHOR
  ** OF THIS SOFTWARE ASSUMES _NO_ RESPONSIBILITY FOR ANY
  ** CONSEQUENCE RESULTING FROM THE USE MODIFICATION OR
  ** REDISTRIBUTION OF THIS SOFTWARE
  **/
  
  package comkindani;
  
  //import javaxservletServletContext;
  import javautil*;
  import javaioInputStream;
  import javaioIOException;
  import javaioBufferedInputStream;
  import javaioFileInputStream;
  
  /**
  * 使用JSE API読取Properties文件的六種方法
  * User: SYNFORM
  * Date: //
  * Time: ::
  * To change this template use File | Settings | File Templates
  */
  public class JProperties {
  
  public final static int BY_PROPERTIES = ;
  public final static int BY_RESOURCEBUNDLE = ;
  public final static int BY_PROPERTYRESOURCEBUNDLE = ;
  public final static int BY_CLASS = ;
  public final static int BY_CLASSLOADER = ;
  public final static int BY_SYSTEM_CLASSLOADER = ;
  
  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);
  pload(in);
  } else if (type == BY_RESOURCEBUNDLE) {
  ResourceBundle rb = ResourceBundlegetBundle(name LocalegetDefault());
  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 (JPropertiesclassequals(new JProperties()getClass()));
  in = JPropertiesclassgetResourceAsStream(name);
  assert (in != null);
  pload(in);
  //    return new JProperties()getClass()getResourceAsStream(name);
  } else if (type == BY_CLASSLOADER) {
  assert (JPropertiesclassgetClassLoader()equals(new JProperties()getClass()getClassLoader()));
  in = JPropertiesclassgetClassLoader()getResourceAsStream(name);
  assert (in != null);
  pload(in);
  //     return new JProperties()getClass()getClassLoader()getResourceAsStream(name);
  } else if (type == BY_SYSTEM_CLASSLOADER) {
  in = ClassLoadergetSystemResourceAsStream(name);
  assert (in != null);
  pload(in);
  }
  
  if (in != null) {
  inclose();
  }
  return p;
  
  }
  
  // servlet used
  /*
  public static Properties loadProperties(ServletContext context String path) throws IOException {
  assert (context != null);
  InputStream in = contextgetResourceAsStream(path);
  assert (in != null);
  Properties p = new Properties();
  pload(in);
  inclose();
  return p;
  }
  */
  
  // support class
  
  /**
  * ResourceBundle Adapter class
  */
  public static class ResourceBundleAdapter extends Properties {
  public ResourceBundleAdapter(ResourceBundle rb) {
  assert (rb instanceof javautilPropertyResourceBundle);
  thisrb = rb;
  javautilEnumeration e = rbgetKeys();
  while (ehasMoreElements()) {
  Object o = enextElement();
  thisput(o rbgetObject((String) o));
  }
  }
  
  private ResourceBundle rb = null;
  
  public ResourceBundle getBundle(String baseName) {
  return ResourceBundlegetBundle(baseName);
  }
  
  public ResourceBundle getBundle(String baseName Locale locale) {
  return ResourceBundlegetBundle(baseName locale);
  }
  
  public ResourceBundle getBundle(String baseName Locale locale ClassLoader loader) {
  return ResourceBundlegetBundle(baseName locale loader);
  }
  
  public Enumeration<String> getKeys() {
  return rbgetKeys();
  }
  
  public Locale getLocale() {
  return rbgetLocale();
  }
  
  public Object getObject(String key) {
  return rbgetObject(key);
  }
  
  public String getString(String key) {
  return rbgetString(key);
  }
  
  public String[] getStringArray(String key) {
  return rbgetStringArray(key);
  }
  
  protected Object handleGetObject(String key) {
  return ((PropertyResourceBundle) rb)handleGetObject(key);
  }
  
  }
  
  }
  
  
  JPropertiesTestjava文件
  
  /**
  ** This program is free software
  **
  ** You may redistribute it and/or modify it under the terms of the GNU
  ** General Public License as published by the Free Software Foundation
  ** Version of the license should be included with this distribution in
  ** the file LICENSE as well as l If the license is not
  ** included with this distribution you may find a copy at the FSF web
  ** site at or or you may write to the
  ** Free Software Foundation Mass Ave Cambridge MA USA
  **
  ** THIS
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19664.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.