- 浏览: 189676 次
- 性别:
- 来自: QD
文章分类
- 全部博客 (127)
- Struts2 (8)
- Web (27)
- 计算机基础 (2)
- 面试 (2)
- JQuery (4)
- MySQL (1)
- SQL (1)
- AJAX (3)
- Java (17)
- Javascript (36)
- 情感 (0)
- Oracle (7)
- Spring (5)
- FreeMarker (2)
- JSON (1)
- 表达式 (1)
- 线程 (4)
- WebService (10)
- MyEclipse (2)
- LDAP (1)
- Tomcat (1)
- NIO (1)
- Linux (1)
- ExtJS (4)
- Android (1)
- Dojo (2)
- Maven (9)
- Ant (7)
- 分布式 (1)
- Intellij IDEA (1)
最新评论
-
northc:
米饭军 写道如果文件已存在怎样避免应该会覆盖的
用Ant scp往远程linux传文件 -
米饭军:
如果文件已存在怎样避免
用Ant scp往远程linux传文件 -
luis025:
不支持列隐藏 硬伤
ExtJS4.0 分享Grid导出Excel插件(3.28更新支持4.1) -
rgbhje:
楼主大神,我把你的代码简单改了一小下,在4.2可以跑了
ExtJS4.0 分享Grid导出Excel插件(3.28更新支持4.1) -
rgbhje:
楼主大神,我把你的代码简单改了一小下,在4.2可以跑了
ExtJS4.0 分享Grid导出Excel插件(3.28更新支持4.1)
使用J2SE API读取Properties文件的六种方法
InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in);
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in); 4。使用class变量的getResourceAsStream()方法 示例: InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in); 5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in); 6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例: InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); 补充 Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例: InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in); 完整的示例,可以参考附件文件 如何上传文件,谁知道请告诉以下。 只好把source都贴上来了 JProperties.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 int 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 IOException { Properties p = new Properties(); InputStream in = null; if (type == BY_PROPERTIES) { in = new BufferedInputStream(new FileInputStream(name)); assert (in != null); p.load(in); } else if (type == BY_RESOURCEBUNDLE) { ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 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 (JProperties.class.equals(new JProperties().getClass())); in = JProperties.class.getResourceAsStream(name); assert (in != null); p.load(in); // return new JProperties().getClass().getResourceAsStream(name); } else if (type == BY_CLASSLOADER) { assert (JProperties.class.getClassLoader().equals(new JProperties().getClass().getClassLoader())); in = JProperties.class.getClassLoader().getResourceAsStream(name); assert (in != null); p.load(in); // return new JProperties().getClass().getClassLoader().getResourceAsStream(name); } else if (type == BY_SYSTEM_CLASSLOADER) { in = ClassLoader.getSystemResourceAsStream(name); assert (in != null); p.load(in); } if (in != null) { in.close(); } return p; } // ---------------------------------------------- servlet used // ---------------------------------------------- support 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.getObject((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 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) { 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.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:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.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:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties"; p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE); assertEquals(value, p.getProperty(key)); assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key)); name = "\\com\\kindani\\test\\LocalStrings.properties"; p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER); assertEquals(value, p.getProperty(key)); name = "\\com\\kindani\\test\\LocalStrings.properties"; p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER); assertEquals(value, p.getProperty(key)); name = "test\\LocalStrings.properties"; p = JProperties.loadProperties(name, JProperties.BY_CLASS); 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: ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Java对properties配置文件的操作
package com.yorsun; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; 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 = filename; File file = new File(filename); try { in = new FileInputStream(file); p = new Properties(); p.load(in); in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.err.println("配置文件config.properties找不到!"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.err.println("读取配置文件config.properties错误!"); e.printStackTrace(); } } public static String getConfigFile(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.equals("")) { 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 { try { File f = new File(filename); out = new FileOutputStream(f); p.store(out, description); out.close(); } catch (IOException ex) { throw new Exception("无法保存指定的配置文件:" + filename); } } public void saveFile(String filename) throws Exception{ saveFile(filename,""); } public void saveFile() throws Exception{ if(filename.length()==0) throw new Exception("需指定保存的配置文件名"); saveFile(filename); } public void deleteValue(String value){ p.remove(value); } public static void main(String args[]){ String file="/eclipse/workspace/NewsTest/WEB-INF/config.properties"; // String file="D:\\eclipse\\workspace\\NewsTest\\WEB-INF\\config.properties"; PropertiesUnit pu=new PropertiesUnit(file); pu.list(); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.test.TestClass; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadPropertiesFile ...{ public void readPropertiesFile(String fileName) throws FileNotFoundException ...{ String str = ""; Properties prop = new Properties(); InputStream stream = null; //读取这个类在同一包中的properties文件 //stream = getClass().getClassLoader().getResourceAsStream(fileName); System.out.println("path:" + getClass().getResource(fileName)); //读取SRC下的的properties文件 String path = getClass().getResource("/").getPath(); stream = new BufferedInputStream(new FileInputStream(new File(path+fileName))); try ...{ prop.load(stream); str = prop.getProperty("localname"); System.out.println("localname:" + str); System.out.println("properties:" + prop); stream.close(); } catch (IOException e) ...{ // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) throws FileNotFoundException ...{ // TODO Auto-generated method stub new ReadPropertiesFile().readPropertiesFile("config.properties"); } } -------------------------------------------------------------------------------------------------------------------------------------- //=================sprin配置文件================================ <bean id="userService" class="com.thtf.ezone.ezesb.jmx.admin.service.impl.UserServiceImpl"> <property name="filePath" value="config/userInfo.properties" /> </bean> //=================java文件================================
package com.thtf.ezone.ezesb.jmx.admin.service.impl; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class UserServiceImpl implements UserService { String filePath = null; public void setFilePath(String filePath) { this.filePath = filePath; } public static void main(String dd[])throws Exception{ Properties p = new Properties(); FileInputStream ferr=new FileInputStream((getClass().getClassLoader() .getResource("") + filePath).toString().substring(6));// 用subString(6)去掉:file:/try{ p.load(ferr); ferr.close(); Set s = p.keySet(); Iterator it = s.iterator(); while(it.hasNext()){ String id = (String)it.next(); String value = p.getProperty(id); System.out.println(id+":="+value); } }catch(Exception e){ e.printStackTrace(); } } } //==============databaseconfing.properties 文件=====================
#---------------------------------- # sql server 2000数据厍连接信息 #---------------------------------- dp.sd.DataBaseTyp=mssql DataBaseHost=127.0.0.1:1433 DataBase=formpro UserName=sa PassWord=01 #---------------------------------- # mysql 数据厍连接信息 #---------------------------------- #DataBaseHost=127.0.0.1:3306 #DataBaseTyp=mysql #DataBase=snow #UserName=root #PassWord=01 //==========================运行结果======================= PassWord:=01 |
发表评论
-
Java桌面程序打包成exe可执行文件(转)
2012-12-27 00:56 1181前言: 我们都知道Java可以将二进制程序打包成可执行 ... -
Java:应用Observer接口实践Observer模式(转)
2012-12-27 00:13 814转自:http://zhangjunhd.blog.51cto ... -
使用Java继承与泛型特性创建高效与安全的DAO对象
2012-06-27 17:15 0现在无论做什么应用,基本上都会有涉及对象持久化这方面的操 ... -
Java 批量插入数据(Oracle)
2012-03-28 16:21 2956//批量添加20000条数据用时8秒。 try { ... -
httpclient ,HtmlParser
2011-12-26 17:40 0有时间研究下吧... -
request 获取路径
2011-12-26 17:38 1103request.getRequestURI() /j ... -
ServletContext对象应用
2011-12-07 14:53 1299ServletContext对象是由谁创建的? 由Serv ... -
配置listener监听器(HttpSessionListener和HttpSessionBindingListener)细说
2011-12-07 14:52 1261from:http://blog.csdn.net/opnmz ... -
LDAP的特定错误集合
2011-11-28 11:29 2911LDAP的特定错误Error: com.macromedia. ... -
Filter多线程小细节
2011-11-26 14:50 1194在使用fileter的时候 如果在中间的位置需要fil ... -
JDBC调用Oracle的存储过程
2011-11-10 19:10 849本文主要是总结 如何实现 JDBC调用Oracle的存储过 ... -
ThreadLocal<T>总结补充
2011-11-06 17:47 1778原文地址: http://www.iteye.com/ ... -
关于 ThreadLocal
2011-11-06 17:26 1171ThreadLoca ... -
(转)Java编程中影响性能的一些特点
2011-11-04 11:42 623文中将提供一些在Java编程中影响性能的一些特点,为了能够 ... -
JSON与JAVA数据的转换
2011-10-21 12:24 890JSON-lib这个Java类包用于把bean,ma ... -
(转)Java内部类的用法
2011-10-21 09:05 741一、 定义 放在一个类的内部的类我们就叫内部类。 二、 作用 ... -
Java动态代理
2011-10-19 12:00 745代理模式 代理模式的 ... -
利用java的反射机制来访问类中的私有成员变量,私有构造方法,私有方法
2011-09-24 11:03 23831. 准备一个演示类 package my ...
相关推荐
java 读取properties文件代码
java读写properties配置文件
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
JAVA读取properties的值,JAVA读取properties文件,JAVA读取properties的内容
Java读取Properties文件的六种方法
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
java实现properties文件读取
java如何读取properties配置文件
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
解决JAVA读取properties中文乱码问题
java完美读取properties文件
读取properties文件返回map
java读取properties文件,没有用到了第三方jar包,方便实用
NULL 博文链接:https://xieke90.iteye.com/blog/2233046
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
java对properties配置文件的读和写
excel与properties文件相互转换