当前位置:首页 > 全部子站 > IT > 思科认证

使用J2SEAPI读取Properties文件的方法

来源:长理培训发布时间:2017-12-21 14:18:38

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

  示例:

  cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"> InputStream in = 
(new FileInputStream(name));
p.load(in);

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

  示例: 

  ResourceBundle rb =
Locale.getDefault());

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

  示例: 

  InputStream in = 
ResourceBundle rb =

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

  示例: 

  InputStream in = 
Properties p = new Properties();

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

  示例: 

  InputStream in = 
.getResourceAsStream(name);
p.load(in);

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

  示例: 

  InputStream in = 
Properties p = new Properties();

  补充

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

  示例: 

  InputStream in = 
Properties p = new Properties();

  JProperties.java文件 

  /**
** 
/or modify it under the terms of the GNU
published by the Free Software Foundation.
be included with this distribution in
License.html. If the license is not
you may find a copy at the FSF web
'www.fsf.org', or you may write to the
675 Mass Ave, Cambridge, MA 02139 USA.
** THIS SOFTWARE IS PROVIDED
** NOT EVEN THE IMPLIED WARRANTY
** OF THIS SOFTWARE, ASSUMES
** CONSEQUENCE RESULTING FROM
** REDISTRIBUTION OF THIS SOFTWARE. 

  package com.kindani;

  //import javax.servlet.ServletContext;
import java.io.InputStream;
import java.io.BufferedInputStream;

  /**
* User: SYNFORM
* Time: 18:40:55
| Settings | File Templates.
public class JProperties 
BY_PROPERTIES = 1;
BY_RESOURCEBUNDLE = 2;
BY_PROPERTYRESOURCEBUNDLE = 3;
BY_CLASS = 4;
BY_CLASSLOADER = 5;
BY_SYSTEM_CLASSLOADER = 6;

   public final static Properties 
final int type) throws IOException 
Properties p = new Properties();
if (type == BY_PROPERTIES)
in = new BufferedInputStream
assert (in != null);
} else if (type == BY_RESOURCEBUNDLE) 
ResourceBundle rb = 
Locale.getDefault());
p = new ResourceBundleAdapter(rb);
else if
{
(new FileInputStream(name));
ResourceBundle rb =
p = new ResourceBundleAdapter(rb);
else if (type == BY_CLASS)
assert (JProperties.class.equals(new JProperties().getClass()));
getResourceAsStream(name);
p.load(in);
getResourceAsStream(name);
{
getClassLoader().equals
getClassLoader()));

assert (in != null);
// return new JProperties().getClass().
} else if (type == BY_SYSTEM_CLASSLOADER)
in = ClassLoader.
assert (in != null);
}

   if (in != null) 
in.close();
return p;

   }

   //servlet used
public static Properties loadProperties
throws IOException 
assert (context != null);
getResourceAsStream(path);
Properties p = new Properties();
in.close();
}

   //support class

   /**
*/
ResourceBundleAdapter extends Properties
public ResourceBundleAdapter
{
java.util.PropertyResourceBundle);
java.util.Enumeration e = rb.getKeys();
{
this.put(o, rb.getObject((String) o));
}|||    private ResourceBundle rb = null;

   public ResourceBundle 
{
}

   public ResourceBundle getBundle
{
(baseName, locale);

   public ResourceBundle 
ClassLoader loader) 
return ResourceBundle.getBundle
}

   public Enumeration getKeys()
return rb.getKeys();

   public Locale getLocale() 
return rb.getLocale();

   public Object getObject(String key) 
return rb.getObject(key);

   public String getString(String key) 
return rb.getString(key);

   public String getStringArray(String key) 
return rb.getStringArray(key);

   protected Object handleGetObject(String key) 
return ((PropertyResourceBundle)
}

   }

  }

  JPropertiesTest.java文件 

  /**
** 
or modify it under the terms of the GNU
published by the Free Software Foundation.
be included with this distribution in
License.html. If the license is not
you may find a copy at the FSF web
or you may write to the
675 Mass Ave, Cambridge, MA 02139 USA.
** THIS SOFTWARE IS PROVIDED 
** NOT EVEN THE IMPLIED WARRANTY 
** OF THIS SOFTWARE, ASSUMES 
** CONSEQUENCE RESULTING FROM 
** REDISTRIBUTION OF THIS SOFTWARE. 
package com.kindani.test;

  import junit.framework.*;

  //import javax.servlet.ServletContext;

  public class JPropertiesTest extends TestCase 
JProperties jProperties;
String value = "Hello World!";

   public void testLoadProperties() 
String name = null;
name = "C:""IDEAP""Properties4Methods
""LocalStrings.properties";
(name, JProperties.BY_PROPERTIES);
(value, p.getProperty(key));
p = JProperties.loadProperties(name,
assertEquals(value, p.getProperty(key));
((JProperties.ResourceBundleAdapter)p)
name = "C:""IDEAP""Properties4Methods""
LocalStrings.properties";
assertEquals(value, p.getProperty(key));
((JProperties.ResourceBundleAdapter)p).
LocalStrings.properties";
assertEquals(value, p.getProperty(key));

   name = """com""kindani""test""
p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);

   name = "test""LocalStrings.properties";
JProperties.BY_CLASS);
}

  /*
throws Exception 
ServletContext context = null;
Properties p = null;
/LocalStrings.properties";
assertEquals(value, p.getProperty(key));
*/
properties文件与JPropertiesTest.
LocalStrings.properties文件
v 1.1 2000/08/17 00:57:52 horwat Exp $

  # Default localized resources
# This locale is en_US

  helloworld.title=Hello World!

  requestinfo.title=Request 
requestinfo.label.method=Method:
requestinfo.label.protocol=Protocol:
requestinfo.label.remoteaddr=Remote Address:

  requestheader.title=Request Header Example

  requestparams.title=Request
requestparams.params-in-req=Parameters
requestparams.no-params=No Parameters,
requestparams.firstname=First Name:

  cookies.title=Cookies Example
sending the following cookies:
isn't sending any cookies
cookie to send to your browser
cookies.value=Value:
following cookie to your browser:

  sessions.title=Sessions Example
sessions.created=Created:
Last Accessed:
data is in your session:
to your session
Session Attribute:
of Session Attribute:

责编:罗莉

发表评论(共0条评论)
请自觉遵守互联网相关政策法规,评论内容只代表网友观点,发表审核后显示!

国家电网校园招聘考试直播课程通关班

  • 讲师:刘萍萍 / 谢楠
  • 课时:160h
  • 价格 4580

特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关

配套通关班送国网在线题库一套

课程专业名称
讲师
课时
查看课程

国家电网招聘考试录播视频课程

  • 讲师:崔莹莹 / 刘萍萍
  • 课时:180h
  • 价格 3580

特色解密新课程高频考点,免费学习,助力一次通关

配套全套国网视频课程免费学习

课程专业名称
讲师
课时
查看课程
在线题库
面授课程更多>>
图书商城更多>>
在线报名
  • 报考专业:
    *(必填)
  • 姓名:
    *(必填)
  • 手机号码:
    *(必填)
返回顶部