当前位置:首页 » 编程语言 » java取properties

java取properties

发布时间: 2022-03-04 05:47:14

java如何读取.properties文件的数据

在prop包下建立LoadProp.java文件。 3.有很多方法来读取.properties文件,现将主要方法罗列出来: a.通过class的getResourceAsStream()方法来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } 一定要注意的是,class里的getResourceAsStream()方法里参数的类路径一定要在前面加上"/",否则会报错 b.使用class的getClassLoader()方法所得的ClassLoader的getResourceAsStream()来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } ClassLoader的getResourceAsStream()方法与Class的getResourceAsStream()方法有点区别,在这里一定不要在类路径前面加上"/",否则会报错,是不是很奇怪。 c.使用ResourceBundle来读取 package prop; import java.util.ResourceBundle; public class LoadProp { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config/a"); System.out.println(rb.getString("name")); System.out.println(rb.getString("age")); } } 注意,getBundle()方法里的参数,是baseName,不要把后缀名写出来,并且不要加"/"。 好了,这是读取.properties文件的几种主要方法,还有其他的方法,基本上都大同小异。

⑵ java如何读取properties文件

../conf/config.properties
还有src是程序运行的根目录,建议把所有与程序相关的都放到src里.不要放在外面

⑶ java怎么获取properties文件中的值

按照读取属性文件的方法即可,如下:

⑷ java中如何取得properties文件的路径

首先你为什么要重命名src,这个是放源码的包,另外你之所以取不到是因为你当前路径(classpath)下没有src啊,src是上一层,你肯定找不到,

⑸ java代码怎么获取properties文件的内容


importjava.util.Properties;

publicclassPropertiesUtil{

privatestaticPropertiesinit=null;
privatestaticPropertiesutil=null;
privatestaticPropertieschid=null;

(){
if(init==null){
try{
init=newProperties();
init.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("init.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returninit;
}

(){
if(util==null){
try{
util=newProperties();
util.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("util.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnutil;
}

(){
if(chid==null){
try{
chid=newProperties();
chid.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("chid.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnchid;
}

/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticStringget(Stringkey,Stringdef){
Stringval=getInit().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
publicstaticlonggetlong(Stringkey,longdef)
{
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticintget(Stringkey,intdef){
try{
def=Integer.parseInt(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}

publicstaticlongget(Stringkey,longdef){
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}

/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticStringgetUtil(Stringkey,Stringdef){
Stringval=getUtil().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}

publicstaticlonggetUtil(Stringkey,longdef){
longval=Long.parseLong(getUtil().getProperty(key));
if(val==0){
returndef;
}
returnval;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
(Stringkey,Stringdef){
Stringval=getChid().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
importcom.jinlou.util.PropertiesUtil;
publicclassTest{
publicstaticvoidmain(String[]args){
//从配置文件中去key=test的value如果配置文件中无此key则返回默认值
Stringtest=PropertiesUtil.get("test","默认值");

System.out.println(test);
}
}

⑹ java怎么读取properties文件

/*** 用来测试获取配置文件的类* @author BaiKeyang**/public class ResourceUtils {public static String getValue(String file, String key) {        String value;        // 未取到值,从properites文件中查找        try {         ResourceBundle bundle = ResourceBundle.getBundle(file);            value = bundle.getString(key);            if (value==null){                throw new RuntimeException("没有该属性的值:"+key);            }            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");            return value;        } catch (Throwable e) {         System.err.println(e);            return null;        }    }public static void main(String[] args) {String resourceFile = "config.dataBase";//      创建一个默认的ResourceBundle对象   //        ResourceBundle会查找包config下的dataBase.properties的文件   //        config是资源的包名,它跟普通java类的命名规则完全一样:   //        - 区分大小写   //        - 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样   //        - 资源文件必须位于指定包的路径之下(位于所指定的classpath中)   //        假如你是在非Web项目中使用,则一定要写资源文件的路径,也就是包路径必须存在。//        如果是Web项目,不写包路径可以,此时将资源文件放在WEB-INF\classes\目录下就可以。String url = getValue(resourceFile, "db.url");System.out.println(url);}}

⑺ 在java中如何读取properties文件

最常用读取properties文件的方法
InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");
Java中获取路径方法
获取路径的一个简单实现
反射方式获取properties文件的三种方式

1 反射方式获取properties文件最常用方法以及思考:
Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:

InputStream in = getClass().getResourceAsStream("资源Name");

这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。

问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。

那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊--
取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

/**
* 读取Properties文件的例子
* File: TestProperties.java
* User: leimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
private static String param1;
private static String param2;

static {
Properties prop = new Properties();
InputStream in = Object. class .getResourceAsStream( "/test.properties" );
try {
prop.load(in);
param1 = prop.getProperty( "initYears1" ).trim();
param2 = prop.getProperty( "initYears2" ).trim();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 私有构造方法,不需要创建对象
*/
private TestProperties() {
}

public static String getParam1() {
return param1;
}

public static String getParam2() {
return param2;
}

public static void main(String args[]){
System.out.println(getParam1());
System.out.println(getParam2());
}
}

运行结果:

151
152
当然,把Object.class换成int.class照样行,呵呵,大家可以试试。

另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法
2 获取路径的方式:
File fileB = new File( this .getClass().getResource( "" ).getPath());

System. out .println( "fileB path: " + fileB);

2.2获取当前类所在的工程名:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span>
/**

*获取项目的相对路径下文件的绝对路径

*

* @param parentDir

*目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or

* bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径

* @param fileName

*文件名

* @return一个绝对路径

*/

public static String getPath(String parentDir, String fileName) {

String path = null;

String userdir = System.getProperty("user.dir");

String userdirName = new File(userdir).getName();

if (userdirName.equalsIgnoreCase("lib")

|| userdirName.equalsIgnoreCase("bin")) {

File newf = new File(userdir);

File newp = new File(newf.getParent());

if (fileName.trim().equals("")) {

path = newp.getPath() + File.separator + parentDir;

} else {

path = newp.getPath() + File.separator + parentDir

+ File.separator + fileName;

}

} else {

if (fileName.trim().equals("")) {

path = userdir + File.separator + parentDir;

} else {

path = userdir + File.separator + parentDir + File.separator

+ fileName;

}

}

return path;

}

4 利用反射的方式获取路径:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" );

InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" );

InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );

⑻ java读取properties文件

Java读取properties文件的方法比较多;
在最常用的读取properties文件的方式--->“通过java.lang.Class类的getResourceAsStream(String name) 方法来实现”;
代码:
InputStream in = getClass().getResourceAsStream("资源Name");

⑼ java读取properties文件问题

把properties文件中的值读取到内存对象Properties中,方便使用;

⑽ java中怎么读取properties

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesTest
{
/*定义静态方法,类名可以直接调用,用于读取properties属性文件中的内容*/
public static void initFile()
{
/*D://a.properties源属性文件的路径和名字*/
File file = new File("D://a.properties");
FileInputStream fis = null;
try
{
/*输入流和属性文件关联*/
fis = new FileInputStream(file);
/*创建属性集对象*/
Properties prop = new Properties();
/*将读取的内容加载到属性集对象中*/
prop.load(fis);
/*返回属性列表中所有键的枚举*/
Enumeration enums = prop.propertyNames();
while (enums.hasMoreElements())
{
/*将每一条属性强制转换为String类型,得到键key*/
String key = (String) enums.nextElement();
/*根据键得到对应的值value(String类型)*/
String value = prop.getProperty(key);
/*输出properties属性文件的内容*/
System.out.println(key+"----"+value);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
/*调用方法读取属性文件中内容*/
PropertiesTest.initFile();
}
}

结果是:
UserPass----
URl----jdbc:microsoft:sqlserver://localhost:1433;databasename=employee
Driver----com.microsoft.jdbc.sqlserver.SQLServerDriver
UserName----sa

热点内容
服务器改id是什么 发布:2025-07-01 10:30:03 浏览:610
php微信开发教程 发布:2025-07-01 10:28:24 浏览:841
访问学者联系导师 发布:2025-07-01 10:27:44 浏览:983
服务器端的http地址 发布:2025-07-01 10:04:35 浏览:176
php好处 发布:2025-07-01 10:01:45 浏览:877
安卓车机什么屏幕好 发布:2025-07-01 09:46:15 浏览:547
编译原理re不能描述的串 发布:2025-07-01 09:34:24 浏览:75
本地电脑怎么用服务器ip 发布:2025-07-01 09:10:03 浏览:831
有趣的python代码 发布:2025-07-01 09:10:02 浏览:362
威龙奇兵ftp 发布:2025-07-01 09:07:38 浏览:694