자바 의 property 프로필 사용법

5832 단어 자바 기반Java


package configuration;
import Java.io.FileInputStream;
import Java.io.FileNotFoundException;
import Java.io.FileOutputStream;
import Java.io.IOException;
import Java.util.Properties;
/** *//**
* properties
* @author Qutr
*
*/
public class Configuration
...{
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;

/** *//**
* Configuration
*/
public Configuration()
...{
propertie = new Properties();
}

/** *//**
* Configuration
* @param filePath +
*/
public Configuration(String filePath)
...{
propertie = new Properties();
try ...{
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) ...{
System.out.println(" ---> !- : ");
ex.printStackTrace();
} catch (IOException ex) ...{
System.out.println(" ---> !");
ex.printStackTrace();
}
}//end ReadConfigInfo(...)

/** *//**
* , key
* @param key
* @return key
*/
public String getValue(String key)
...{
if(propertie.containsKey(key))...{
String value = propertie.getProperty(key);//
return value;
}
else
return "";
}//end getValue(...)

/** *//**
* , key
* @param fileName properties +
* @param key
* @return key
*/
public String getValue(String fileName, String key)
...{
try ...{
String value = "";
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();
if(propertie.containsKey(key))...{
value = propertie.getProperty(key);
return value;
}else
return value;
} catch (FileNotFoundException e) ...{
e.printStackTrace();
return "";
} catch (IOException e) ...{
e.printStackTrace();
return "";
} catch (Exception ex) ...{
ex.printStackTrace();
return "";
}
}//end getValue(...)

/** *//**
* properties key
*/
public void clear()
...{
propertie.clear();
}//end clear();
/** *//**
* key , key properties key value ,
* key , key value
* @param key
* @param value
*/
public void setValue(String key, String value)
...{
propertie.setProperty(key, value);
}//end setValue(...)

/** *//**
* , 。
* @param fileName +
* @param description
*/
public void saveFile(String fileName, String description)
...{
try ...{
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException ioe)...{
ioe.printStackTrace();
}
}//end saveFile(...)

public static void main(String[] args)
...{
Configuration rc = new Configuration(".\config\test.properties");//

String ip = rc.getValue("ipp");// properties
String host = rc.getValue("host");
String tab = rc.getValue("tab");

System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());// properties
System.out.println("ip's length = " + ip.length());
System.out.println("host = " + host);
System.out.println("tab = " + tab);
Configuration cf = new Configuration();
String ipp = cf.getValue(".\config\test.properties", "ip");
System.out.println("ipp = " + ipp);
// cf.clear();
cf.setValue("min", "999");
cf.setValue("max", "1000");
cf.saveFile(".\config\save.perperties", "test");

// Configuration saveCf = new Configuration();
// saveCf.setValue("min", "10");
// saveCf.setValue("max", "1000");
// saveCf.saveFile(".\config\save.perperties");

}//end main()

}//end class ReadConfigInfo

좋은 웹페이지 즐겨찾기