求助,读property配置文件

lenovo_itcast 2010-01-11 04:00:39
//a.property
rut.a=1
rut.b=2
rut.c=3

swt.a=11
swt.b=22
swt.c=33
swt.d=44


输入rut,返回[a,1,b,2,c,3]

返回值放在list里。

如何实现??急等。。。。。
...全文
97 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tuerqizhou 2010-01-12
  • 打赏
  • 举报
回复
我这儿有我写的一个例子
你看看对你有用不?

package teststart.teststrartreader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class TestProperty {
Properties pro = new Properties();

public TestProperty() {
FileInputStream fis = null;
try {
fis = new FileInputStream("./config/config.ini");
pro.load(fis);//通过输入流加载文件到属性集合里面
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String getValue(String key,String def){
String value = pro.getProperty(key,def);
return value;
}

private void setKeyValue(String key,String value) {
pro.setProperty(key, value);
}

//输出到文件
public void saveValue(){
FileOutputStream fos = null;
try {
fos = new FileOutputStream("./config/config.ini");
pro.store(fos, "comments");
System.out.println("写入完成");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* @param args
*/
public static void main(String[] args) {
TestProperty test = new TestProperty();

test.setKeyValue("username","zhoulei");
test.setKeyValue("url","jdbc:oracle:thin:@127.0.0.1:1521:orcl");
test.setKeyValue("password", "oracle");
test.setKeyValue("driver", "oracle.jdbc.driver.OracleDriver");
test.saveValue();

String driver = test.getValue("driver", "def");
String username = test.getValue("username", "zhoulei");
String password = test.getValue("password", "oracle");
String url = test.getValue("url", "oracle");

try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("conn="+conn);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

若鱼1919 2010-01-11
  • 打赏
  • 举报
回复

public class TestProps {
public static void main(String[] args)throws Exception {
Properties props=new Properties();
InputStream in=TestProps.class.getResourceAsStream("a.properties");
props.load(in);
String input="rut";
List result=new ArrayList();
Set set=props.keySet();
for(Iterator it=set.iterator();it.hasNext();){
String key=(String)it.next();
if(key.startsWith(input)){
result.add(key.split("\\.")[1]);
result.add(props.getProperty(key));
}
}
System.out.println(result);
}
}
huntor 2010-01-11
  • 打赏
  • 举报
回复
上面搞错了
[Quote=引用 1 楼 huntor 的回复:]
Java codeProperties p=new Properties();

p.load(new FileReader("a.properties"));
Enumeration<String> keys= p.keys();
ArrayList<String> results=new ArrayList<String>();
while(keys.hasMoreElements()){
String key= keys.nextElement();
if(key.startsWith("rut"){
results.add(key.substring(4));
results.add(p.getProperty(key));
}
}
[/Quote]
Yokubee 2010-01-11
  • 打赏
  • 举报
回复
使用java.util.Properties
遍历a.properties文件找到含有rut的键,然后把键拆成你要的,最后把拆好的和对应的值放入list中
如遍历
Properties prop = new Properties();
prop.put(1, "a");
prop.put(2, "b");
Enumeration enums = prop.keys();
while (enums.hasMoreElements()) {
Object key = enums.nextElement();
System.out.println("key = " + key + " value = " + prop.get(key));
}
huntor 2010-01-11
  • 打赏
  • 举报
回复
Properties p = new Properties();

p.load(new FileReader("a.properties"));
Object[] keys = p.keys();
ArrayList<String> results = new ArrayList<String>();
for(Object k : keys){
String key = (String)k;
if(key.startsWith("rut"){
results.add(key.substring(4));
results.add(p.getProperty(key));
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧