当前位置:首页 » 编程语言 » javaproperties文件读取

javaproperties文件读取

发布时间: 2025-03-28 17:24:11

① 在java中如何读取properties文件

使用java.util.Propertiesx0dx0ax0dx0a1、创建一个Properties对象。x0dx0a2、使用对象的load方法加载你的property文件。x0dx0a3、使用getProperty方法取值。x0dx0a例子:x0dx0apackage com.bill.test;x0dx0ax0dx0aimport java.io.FileInputStream;x0dx0aimport java.util.Properties;x0dx0ax0dx0apublic class Test {x0dx0apublic static void main(String[] args) throws Exception{x0dx0aProperties property = new Properties();x0dx0aproperty.load(new FileInputStream("你的文件位置"));x0dx0aString value = property.getProperty("你的属性的key");x0dx0a//TODO 使用value...x0dx0a}x0dx0a}

② java读取properties文件

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

③ 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);
}
}
}

④ 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();

热点内容
迷你世界登录的时候密码是多少 发布:2025-03-31 14:58:59 浏览:350
VNC电脑版服务器怎么用 发布:2025-03-31 14:58:09 浏览:417
傲剑脚本 发布:2025-03-31 14:51:10 浏览:494
阿里邮箱怎么配置ipad 发布:2025-03-31 14:49:39 浏览:10
犯罪大师侦探密码答案是什么 发布:2025-03-31 14:48:50 浏览:478
新国都g2初始化密码多少 发布:2025-03-31 14:38:38 浏览:173
php多线程教程 发布:2025-03-31 14:37:21 浏览:368
数据存储技术的发展 发布:2025-03-31 14:28:45 浏览:508
元组数据库 发布:2025-03-31 14:25:00 浏览:714
手机文件android在哪 发布:2025-03-31 14:22:38 浏览:817