1、 6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 InputStream in = ClassLoader.getSystemResourceAsStream(name); 补充 Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法InputStream in = context.getResourceAsStream(path); 完整的示例,可以参考附件文件 如何上传文件,谁知道请告诉以下。 只好把source都贴上来了 JProperties.
2、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; public class JProperties public final static int BY_PROPERTIES = 1; public final static in
3、t 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
4、 IOException InputStream in = null; if (type = BY_PROPERTIES) in = new BufferedInputStream(new FileInputStream(name); assert (in != null); else if (type = BY_RESOURCEBUNDLE) assert (rb ! p = new ResourceBundleAdapter(rb); else if (type = BY_PROPERTYRESOURCEBUNDLE) else if (type = BY_CLASS) assert (J
5、Properties.class.equals(new JProperties().getClass(); in = JProperties.class.getResourceAsStream(name); / return new JProperties().getClass().getResourceAsStream(name); else if (type = BY_CLASSLOADER) assert (JProperties.class.getClassLoader().equals(new JProperties().getClass().getClassLoader(); in
6、 = JProperties.class.getClassLoader().getResourceAsStream(name); / return new JProperties().getClass().getClassLoader().getResourceAsStream(name); else if (type = BY_SYSTEM_CLASSLOADER) in = ClassLoader.getSystemResourceAsStream(name); if (in != null) in.close(); return p; / - servlet used / - suppo
7、rt 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.g
8、etObject(String) o); private ResourceBundle rb = null; public ResourceBundle getBundle(String baseName) return ResourceBundle.getBundle(baseName); public ResourceBundle getBundle(String baseName, Locale locale) return ResourceBundle.getBundle(baseName, locale); public ResourceBundle getBundle(String
9、 baseName, Locale locale, ClassLoader loader) return ResourceBundle.getBundle(baseName, locale, loader); 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
10、) return rb.getString(key); public String getStringArray(String key) return rb.getStringArray(key); protected Object handleGetObject(String key) return (PropertyResourceBundle) rb).handleGetObject(key); JPropertiesTest.java文件 package com.kindani.test; import junit.framework.*; import com.kindani.JPr
11、operties; 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; name = C:IDEAPProperties4MethodssrccomkindanitestLocalStrings
12、.properties p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES); assertEquals(value, p.getProperty(key);com.kindani.test.LocalStrings p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE); assertEquals(value,(JProperties.ResourceBundleAdapter)p).getString(key); p = JProperti
13、es.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);comkindanitestLocalStrings.properties p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER); p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);testLocalStrings.properties p = JProperties.loadProperties(na
14、me, JProperties.BY_CLASS); 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 Infor
15、mation 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
16、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
17、browser isnt 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
18、=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:-Java对properties配置文件的操作 package com.yorsun;import java.io.File; import java.io.FileNotFoundExcepti
19、on; import java.io.FileOutputStream;import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; public class PropertiesUnit private String filename;private Properties p;private FileInputStream in;private FileOutputStream out;public PropertiesUnit(String filename) this.filename = file
20、name; File file = new File(filename); try in = new FileInputStream(file); p = new Properties(); catch (FileNotFoundException e) / TODO Auto-generated catch block System.err.println(配置文件config.properties找不到!); e.printStackTrace(); catch (IOException e) 读取配置文件config.properties错误!public static String g
21、etConfigFile(HttpServlet hs) return getConfigFile(hs, config.properties private static String getConfigFile(HttpServlet hs, String configFileName) String configFile = ServletContext sc = hs.getServletContext(); configFile = sc.getRealPath(/ + configFileName); if (configFile = null | configFile.equal
22、s() configFile = + configFileName; / TODO Auto-generated method stub return configFile;public void list() p.list(System.out);public String getValue(String itemName) return p.getProperty(itemName);public String getValue(String itemName, String defaultValue) return p.getProperty(itemName, defaultValue);public void setValue(String itemName, String value) p.setProperty(itemName, value);public void saveFile(String filename, String description) throws Exception File f = new File(filename); out = new FileOutputStream(f)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1