导航:首页 > 编程语言 > java怎么读取property

java怎么读取property

发布时间:2025-05-26 15:09:41

1. java调用properties文件的问题

Java可使用Properties类读写properties,具体说明如下:

1.Properties类与Properties配置文件
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2.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();
如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。
注释信息后面是属性文件的当前保存时间信息。
(3)getProperty/setProperty
这两个方法是分别是获取和设置属性信息。

3.代码实例
属性文件a.properties如下:
name=root
pass=liu
key=value
读取a.properties属性列表,与生成属性文件b.properties。代码如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
try {
// 读取属性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream("a.properties"));
// /加载属性列表
Properties prop = new Properties();
prop.load(in);
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + prop.getProperty(key));
}
in.close();
// /保存属性到b.properties文件
FileOutputStream oFile = new FileOutputStream("b.properties", true);// true表示追加打开
prop.setProperty("phone", "10086");
prop.store(oFile, "The New properties file");
oFile.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

2. properties文件怎么打开啊

1、先讲一下怎么读取项目内的配置文件,properties文件,里面有两个键值对name:爬楼高手和age:37。

3. jsp中.properties文件读取得步骤是什么

这是properties 文件内容
user=sa
password=sa
url=jdbc:sqlserver://localhost:1433;DatabaseName=tempdb
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
这里是建的一个类,
import java.io.InputStream;
import java.util.Properties;
public class Env extends Properties//继承Properties (java。util 下的)
{
private static Env env=new Env();//创建一个私有的属性,就是自己本身,顺便实例化
public static Env getEnv()//一个公共类型,其他类可以访问到
{
return env;
}
private Env()
{
try {
InputStream is=getClass().getResourceAsStrea("jdbc.properties");//这里就相当是一个文件流,设置 从那个文件读取
load(is);
//把这个文件流加载到 这个类里
} catch (Exception e) {
e.printStackTrace();
}
}
}
//下面是测试类得到 properties 文件中的值,因为properties文件中是以 键值贮存 的
所以用下面的方法得到 properties 中的值
String driver=Env.getEnv().getProperty("driver");

String user=Env.getEnv().getProperty("user");

String url=Env.getEnv().getProperty("url");

String sa=Env.getEnv().getProperty("sa");

String pwd=Env.getEnv().getProperty("password");

System.out.println(driver);

4. 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();
}

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

5. property在Java中的用法

JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:
1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

6. JAVA中如何读取src下所有的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怎么读取property相关的资料

热点内容
ps邀请函制作教程 浏览:23
rp管螺纹怎么编程 浏览:823
linuxssh下载文件 浏览:765
如何进行光盘数据迁移 浏览:614
编程怎么搭建网站 浏览:481
webjspwebinf 浏览:520
软件系统文件夹怎么用 浏览:126
数据库应用技术中级证书有什么用 浏览:811
数据存储方式有哪些网络存储 浏览:552
苍穹数据服务云都有哪些 浏览:257
简历表格下载word格式百度文库 浏览:674
word修改软件 浏览:266
网络攻击问题有哪些 浏览:446
win10缺少配置文件 浏览:295
php连接数据库的配置文件 浏览:801
jsp获取request中的值 浏览:986
jsp中layershow 浏览:679
贝尔金数据线多少钱 浏览:477
微信扣扣电视剧都没声是怎么回事 浏览:329
如何用电脑复制文件到优盘 浏览:537

友情链接