62,628
社区成员
发帖
与我相关
我的任务
分享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();
}
}
}
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);
}
}
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));
}
}