导航:首页 > 编程语言 > java取properties

java取properties

发布时间:2022-07-19 01:29:22

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中如何读取src下所有的properties文件

1.使用java.util.Properties类的load()方法


示例:

//文件在项目下。不是在包下!!

InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;

Properties p = new Properties();

p.load(in) ;

String className2 = p.getProperty("database.driver");

String url = p.getProperty("database.url");

String user = p.getProperty("database.user");

String password = p.getProperty("database.pass");

总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println(path);

InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));

Properties prop = new 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

㈣ java怎么properties的方法

properties是配置文件,主要的作用是通过修改配置文件可以方便的修改代码中的参数,实现不用改class文件即可灵活变更参数。
解释:java运行中java文件会变成class文件,之后无法通过反编译找到原样的代码,这样的话,如果java类中某个参数变更,就很难灵活的实现参数修改,这个时候properties 文件就能很灵活的实现配置,减少代码的维护成本和提高开发效率。

㈤ java读取properties文件

InputStream in = getProperties.class.getClassLoader().getResourceAsStream(
"config.properties");
这一句换个写法试试:

Properties props = new Properties();
String url = this.getClass().getClassLoader().getResource(
"config.properties").toString().substring(6);
String empUrl = url.replace("%20", " ");// 如果你的文件路径中包含空格,是必定会报错的
System.out.println(empUrl);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(empUrl));
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

我就是一直这么写的,没问题。
我猜你读取文件为空的原因,是你的文件路径有空格。

㈥ java中怎么读取properties文件

(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

㈦ java 怎么读取properties

/**
* 读取项目resource下的config文件夹中的配置属性文件
*/
private static void setConfigMap() {
String filePath = classPath + configPath;
filePath = PathUtils.rebuild(filePath);
List<String> list = findFiles(filePath);

Map<String, String> tmpConfigMap = new HashMap<String, String>();
for (String configName : list) {
Properties props = getProperties(filePath + configName);
loadPropToMap(props);
}
}

㈧ 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相关的资料

热点内容
战双程序员 浏览:479
him触摸编程软件 浏览:929
植物大战僵尸存档怎么转移安卓 浏览:852
java栈的元素 浏览:737
程序员与篮球事件 浏览:675
app反编译不完整 浏览:788
电脑上的文件夹怎么调整 浏览:7
服务器无响应是什么原因呀 浏览:984
wd文档里的app怎么制作 浏览:513
电脑里的文件夹没有了一般能恢复吗 浏览:418
哪里有配加密钥匙的 浏览:210
服务器开不了机怎么把数据弄出来 浏览:958
gif动态图片怎么压缩 浏览:521
黑猴子棒球压缩文件解压密码 浏览:631
如何让app适应不同的手机屏幕大小 浏览:10
苹果手机如何给安卓手机分享软件 浏览:761
苹果电脑怎么运行腾讯云服务器 浏览:59
明日之后沙石堡命令助手 浏览:261
蛋糕店用什么样的app 浏览:877
长安银行信用卡app怎么取现 浏览:635