程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2SE >> J2SE API讀取Properties文件的六種方法(3)

J2SE API讀取Properties文件的六種方法(3)

編輯:J2SE

JPropertIEsTest.Java文件

package com.kindani.test;
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 = "com.kindani.test.LocalStrings";
     p = JProperties.loadProperties(name,JPropertIEs.BY_RESOURCEBUNDLE);
     assertEquals(value, p.getProperty(key));
     assertEquals(value,((JPropertIEs.ResourceBundleAdapter)p).getString(key));
     name = "C:IDEAPPropertIEs4Methodssrccom
kindanitestLocalStrings.propertIEs";
     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
requestinfo.label.method=Method:
requestinfo.label.requesturi=Request URI:
requestinfo.label.protocol=Protocol:
requestinfo.label.pathinfo=Path Info:
requestinfo.label.remoteaddr=Remote Address:
requestheader.title=Request Header Example
requestparams.title=Request Parameters Example
requestparams.params-in-req=Parameters in this request:
requestparams.no-params=No Parameters, Please enter some
requestparams.firstname=First Name:
requestparams.lastname=Last Name:
cookies.title=CookIEs Example
cookies.cookies=Your browser is sending the following cookIEs:
cookies.no-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:
sessions.created=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:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved