java Properties存取操作例子(無註解)


public void saveToFile(String newsContent)
{
Properties prop = new Properties();
try
{
InputStream is = NewsPush.class.getClassLoader().getResourceAsStream("newspath.properties");
prop.load(is);
String newsPath = prop.getProperty("path");
is.close();
OutputStream os = new FileOutputStream(newsPath);
prop.clear();
prop.setProperty("content", newsContent);
prop.store(os, "push news content");
os.close();
}
catch(IOException e)
{
log.debug("saveToFile error:"+e);
e.printStackTrace();
}
}

public String getSavedContent()
{
String content = null;
Properties prop = new Properties();
InputStream is = NewsPush.class.getResourceAsStream("/newspath.properties");
try
{
prop.load(is);
String newsPath = prop.getProperty("path");
is.close();
is = new FileInputStream(newsPath);
prop.load(is);
content = prop.getProperty("content");
is.close();
}
catch(IOException e)
{
log.debug("Get saved content error:"+e);
e.printStackTrace();
}
return content;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章