package util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;
public class ProperHelper {
public static Properties JdbcProperty = null;
@SuppressWarnings("finally")
public static Properties getJdbcProperty(){
try{
if(JdbcProperty == null){
InputStream stream = null;
try{
URL fileUrl = ProperHelper.class.getResource("/jdbc.properties");
String filePath = URLDecoder.decode(fileUrl.getFile(),"UTF-8");
stream = new FileInputStream(filePath);
JdbcProperty = new Properties();
JdbcProperty.load(stream);
}catch(IOException ex){
ex.printStackTrace();
return null;
}finally{
if(stream != null){
stream.close();
stream = null;
}
}
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
return JdbcProperty;
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.setProperty("tetx", "my first love");
OutputStream stream = null;
try {
URL fileUrl = ProperHelper.class.getResource("/jdbc.properties");
String filePath = URLDecoder.decode(fileUrl.getFile(),"UTF-8");
stream = new FileOutputStream(filePath);
prop.store(stream, "Copyright (c) Boxcode Studio");
System.out.println(prop.getProperty("tetx"));
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stream!=null){
stream.close();
stream = null;
}
}
}
}