ImageVerifierCode 换一换
格式:DOCX , 页数:10 ,大小:17.79KB ,
资源ID:20283635      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/20283635.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java读取配置文件做法Word格式文档下载.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java读取配置文件做法Word格式文档下载.docx

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