자바 의 property 프로필 사용법
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
범용 용법 예시앞으로 51CTO 에 정착 해 기술 개발 에 전념 할 테 니 잘 부탁드립니다.다음 코드 는 자신 이 (저자: 이 흥 화) 를 공부 할 때 두 드 린 코드 로 주석 이 완비 되 어 있다. 범용 클래스 Point. ja...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.