从网上下的java怎么没有读写properties文件件

java读取properties文件_百度知道
java读取properties文件
  Java读取properties文件的方法比较多;  在最常用的读取properties文件的方式---&“通过java.lang.Class类的getResourceAsStream(String name) 方法来实现”;  代码:  InputStream in = getClass().getResourceAsStream(&资源Name&);
其他类似问题
为您推荐:
提问者采纳
properties&quotInputStream in = getP
}我就是一直这么写的; &quot。我猜你读取文件为空的原因;
in = new BufferedInputStream(new FileInputStream(empUrl)),是你的文件路径有空格.getClassLoader().getResourceAsStream(
&String url =).getClass(), &config,没问题;).println(empUrl);%20&;&#47.substring(6).printStackTrace();这一句换个写法试试:Properties props = new Properties(); 如果你的文件路径中包含空格.getClassLoader();
String empUrl = url.replace(&quot.getResource(
&config.printStackTrace().properties&
} catch (IOException e1) {
e1.toString(););&#47,是必定会报错的
InputStream in = null.load(in);
} catch (FileNotFoundException e1) {
其他8条回答
/InputStream in =new FileInputStream(
&quot.properties&);config
InputStream in = getProperties.class.getClassLoader().getResourceAsStream(&config.properties&);这句改成InputStream in = getProperties.class.getResourceAsStream(&config.properties&);就可以了
文件放到src下,使用InputStream in = new ClassPathResource(&config.properties&).getInputStream();
InputStream in=getProperties.class.getClassLoader().getResourceAsStream(&/config.properties&);路径前面加一个 / 表示src根目录
静态代码块中不行,之前我也遇到过类似的问题,后来我把static代码块去掉直接写到方法体里面了,就解决了
config.properties 这个文件是在src下面,getProperties.java文件是在com.liuchen包下面,这样可以运行的,我已经试过了。
按楼主的写法,应该是config.properties 位置不对,放到src下面即可,即在 类getProperties 包结构的外面
很明显是你的properties文件路径不对,应该在src目录下
properties的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁使用J2SE API读取Properties文件的六种方法1。使用java.util.Properties类的load()方法 & & & & & &示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); & & & & & & & Properties p = new Properties(); & & & & & & & p.load(in); & & & & & &2。使用java.util.ResourceBundle类的getBundle()方法 & & & & & &示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); & & & & & &3。使用java.util.PropertyResourceBundle类的构造函数 & & & & & &示例: 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.
&&&&&&&&&&&&//import&javax.servlet.ServletC
&&&&&&&&&&&&import&java.util.*;
&&&&&&&&&&&&import&java.io.InputS
&&&&&&&&&&&&import&java.io.IOE
&&&&&&&&&&&&import&java.io.BufferedInputS
&&&&&&&&&&&&import&java.io.FileInputS
&&&&&&&&&&&&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&=&
&&&&&&&&&&&&&&&&&&&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&=&
&&&&&&&&&&&&&&&&&&&&&java.util.Enumeration&e&=&rb.getKeys();
&&&&&&&&&&&&&&&&&&&&&while&(e.hasMoreElements())&{
&&&&&&&&&&&&&&&&&&&&&&&&Object&o&=&e.nextElement();
&&&&&&&&&&&&&&&&&&&&&&&&this.put(o,&rb.getObject((String)&o));
&&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&private&ResourceBundle&rb&=&
&&&&&&&&&&&&&&&&&&&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.
&&&&&&&&&&&&import&junit.framework.*;
&&&&&&&&&&&&import&com.kindani.JP
&&&&&&&&&&&&//import&javax.servlet.ServletC
&&&&&&&&&&&&import&java.util.P
&&&&&&&&&&&&public&class&JPropertiesTest&extends&TestCase&{
&&&&&&&&&&&&JProperties&jP
&&&&&&&&&&&&String&key&=&"helloworld.title";
&&&&&&&&&&&&String&value&=&"Hello&World!";
&&&&&&&&&&&&public&void&testLoadProperties()&throws&Exception&{
&&&&&&&&&&&&&&&&&&&String&name&=&
&&&&&&&&&&&&&&&&&&&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
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.
import&java.io.F
&&&&&&&&&&&&import&java.io.FileInputS
&&&&&&&&&&&&import&java.io.FileNotFoundE
&&&&&&&&&&&&import&java.io.FileOutputS
&&&&&&&&&&&&import&java.io.IOE
&&&&&&&&&&&&import&java.util.P
import&javax.servlet.ServletC
&&&&&&&&&&&&import&javax.servlet.http.HttpS
&&&&&&&&&&&&public&class&PropertiesUnit&{
&&&&&&&&&&&&private&String&
private&Properties&p;
private&FileInputStream&
private&FileOutputStream&
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&=&"/"&+&configFileN
&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&//&TODO&Auto-generated&method&stub
&&&&&&&&&&&&&&&return&configF
&&&&&&&&&&&&}
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.TestC
import&java.io.BufferedInputS
&&&&&&&&&&&&import&java.io.F
&&&&&&&&&&&&import&java.io.FileInputS
&&&&&&&&&&&&import&java.io.FileNotFoundE
&&&&&&&&&&&&import&java.io.IOE
&&&&&&&&&&&&import&java.io.InputS
&&&&&&&&&&&&import&java.util.P
public&class&ReadPropertiesFile&...{
public&void&readPropertiesFile(String&fileName)&throws&FileNotFoundException&...{
&&&&&&&&&&&&&&&&&&&&String&str&=&"";
&&&&&&&&&&&&&&&&&&&&Properties&prop&=&new&Properties();
&&&&&&&&&&&&&&&&&&&&InputStream&stream&=&
&&&&&&&&&&&&&&&&&&&&//读取这个类在同一包中的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)));
&&&&&&&&&&&&&&&&&&&&&&&&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.
import&java.io.FileInputS
&&&&&&&&&&&&import&java.io.IOE
&&&&&&&&&&&&import&java.util.P
public&class&UserServiceImpl&implements&UserService&{
&&&&&&&&&&&&String&filePath&=&
public&void&setFilePath(String&filePath)&{
&&&&&&&&&&&&&&&this.filePath&=&fileP
&&&&&&&&&&&&}
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 & & & & & &DataBaseHost:=127.0.0.1:1433 & & & & & &DataBase:=formpro & & & & & &dp.sd.DataBaseTyp:=mssql & & & & & &UserName:=sa
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区java读取properties配置文件的方法
作者:一羽清宁
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
本文实例讲述了java读取properties配置文件的方法。分享给大家供大家参考。具体分析如下:
这两天做java项目,用到属性文件,到网上查资料,好半天也没有找到一个满意的方法能让我读取到.properties文件中属性值,很是郁闷,网上讲的获取属性值大概有以下方法,以下三种方法逐渐优化,以达到最好的效果以下都以date.properties文件为例,该文件放在src目录下,文件内容为:
startdate=
totalweek=25
public class Stweek {
static private String startdate =
static private String totalweek =
synchronized static public void loads(){
if(startdate == null || totalweek == null)
FileInputStream is =
Properties dbProps = new Properties();
is = new FileInputStream(filepath);
dbProps.load(is);
startdate = dbProps.getProperty("startdate");
totalweek = dbProps.getProperty("totalweek");
catch (Exception e) {
System.err.println("不能读取属性文件. " +
"请确保db.properties在CLASSPATH指定的路径中");
public static String getStartdate() {
if(tartdate==null)
public static String getTotalweek() {
if(startdate==null)
以上方法虽然也能获得配置文件内容,可是其最大的问题就是文件路径的定位(就是代码中的filepath取值问题),当采用绝对定位的时候,如果将工程移到另外一个盘符下运行,就需要修改源代码,否则就会报错,可是如果使用相对路径,当Stweek 类移到另外一个包中时,还是要修改源代码,否则会报错,所以说这个方法局限太大,不好,以下方法二能解决这个问题,可是其还是有些问题
public class Stweek {
InputStream is =
Properties dbProps =
public Stweek() {
// TODO Auto-generated constructor stub
is = getClass().getResourceAsStream("/date.properties");
dbProps = new Properties();
dbProps.load(is);
catch (Exception e) {
System.err.println("不能读取属性文件. " +
"请确保db.properties在CLASSPATH指定的路径中");
public String getStartdate()
String sd =
sd = dbProps.getProperty("startdate");
public String getTotalweek()
String totalweek=
totalweek = dbProps.getProperty("totalweek");
这个方法的好处就是不用指出配置文件的绝对路径,而且不管是将Stweek 类放到另外的包中,还是将整个工程移到到另外的盘符下,代码依然可以正常运行,不会有找不到文件的问题,可是这个方法仍然有一个重大的缺陷,因为我们往往希望配置文件能够缓存在内存中,这样不用每次读取时都要访问硬盘(访问外存太浪费时间),为此我们希望使用静态变量、静态方法来缓存和获得变量,同时能够实现这些值的动态加载(load),那么问题来了,因为getClass().getResourceAsStream("/date.properties"); 这一句只能出现在构造函数中(同鞋可以自己测试一下),动态load中并不能使用这个方法,怎么办呢,且看第三个方法
import java.io.InputS
import java.util.P
public class Stweek {
static private String startdate =
static private String totalweek =
synchronized static public void loads(){
if(startdate == null || totalweek == null)
InputStream is = Stweek.class.getResourceAsStream("/date.properties");
Properties dbProps = new Properties();
dbProps.load(is);
startdate = dbProps.getProperty("startdate");
totalweek = dbProps.getProperty("totalweek");
catch (Exception e) {
System.err.println("不能读取属性文件. " +
"请确保db.properties在CLASSPATH指定的路径中");
public static String getStartdate() {
if(startdate==null)
public static String getTotalweek() {
if(startdate==null)
这个方法不仅能够缓存配置文件内容,还能够做到自动加载配置文件的内容到内存,使用者完全不用考虑手动加载的过程,只需要在需要用到的地方直接调用Stweek.getStartdate()即可(因为是静态方法,事先连对像也不用创建的),这样如果内存中有缓存,函数就会直接读取内存中的数据,节省时间,如果没有缓存也不用担心,系统会自动为你加载,使用者完全不用知道其是如何实现的,只需要知道我能直接调用函数获得想要的值就行了,呵呵,简单吧
备注:(与上文无关,自己的测试)
希望本文所述对大家的java程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 读写properties文件 的文章

 

随机推荐