package com.vci.client.logon.client;
|
|
|
import java.io.BufferedInputStream;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.util.Enumeration;
|
import java.util.Hashtable;
|
import java.util.Properties;
|
|
public class GetOrSetProperty {
|
|
public static String readValue(String filePath, String key) {
|
String value = null;
|
if (key == null) {
|
return null;
|
}
|
Properties props = new Properties();
|
try {
|
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
|
props.load(ips);
|
value = props.getProperty(key);
|
// System.out.println(key + "=" + value);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
value = null;
|
} catch (IOException e) {
|
e.printStackTrace();
|
value = null;
|
} catch (Exception e) {
|
e.printStackTrace();
|
value = null;
|
}
|
return value;
|
}
|
|
public static String[] readValues(String filePath, String[] keys) {
|
String[] values = null;
|
if (keys == null) {
|
return null;
|
}
|
Properties props = new Properties();
|
try {
|
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
|
props.load(ips);
|
int len = keys.length;
|
values = new String[len];
|
for (int i = 0; i < len; i++) {
|
values[i] = props.getProperty(keys[i]);
|
}
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
values = null;
|
} catch (IOException e) {
|
e.printStackTrace();
|
values = null;
|
} catch (Exception e) {
|
e.printStackTrace();
|
values = null;
|
}
|
return values;
|
}
|
|
public static void writeProperties(String filePath, Hashtable<String, String> ht) {
|
Properties props = new Properties();
|
try {
|
Enumeration<String> enumration = ht.keys();
|
OutputStream ops = new FileOutputStream(filePath);
|
while (enumration.hasMoreElements()) {
|
String key = enumration.nextElement();
|
String value = ht.get(key);
|
props.setProperty(key, value);
|
}
|
props.store(ops, "set");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static void writeProperties(String filePath, String paraKey, String paraValue) {
|
Properties props = new Properties();
|
try {
|
OutputStream ops = new FileOutputStream(filePath);
|
props.setProperty(paraKey, paraValue);
|
props.store(ops, "set");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|