我正在研究一个需要以下逻辑的类:
If properties file @ "c:\somepath" (m_writePath) exists
if there is a value for the key offset
getProperty(offset)
else
write offset value
else
create property file ("c:/somepath")
write offset value
本质上这是我当前的设置:
public class myclass {
/**
* The properties file to store offset value.
*/
private static Properties m_offsetProperty = new Properties();
/**
* Process stuff.
*/
private void processStuff() {
File tmpFile = new File(m_writePath + "lastOffset.properties");
if (tmpFile.exists()) {
m_offsetProperty.load(new FileInputStream(m_writePath + "lastOffset.properties"));
m_offset = Long.getLong(m_offsetProperty.getProperty("offset"));
}
else {
createOffsetFile();
m_offset = 0;
setOffsetProperty();
}
}
/**
* Create properties file writePath/offset.properties.
*/
private void createOffsetFile() {
m_offsetProperty.setProperty("offset", String.valueOf(m_offset));
File propFile = new File(m_writePath + "lastOffset.properties");
FileWriter fw = null;
fw = new FileWriter(propFile);
m_offsetProperty.store(fw, null);
}
/**
* Write offset to properties file writePath/offset.properties.
*/
private void setOffsetProperty() {
m_offsetProperty.setProperty("offset", String.valueOf(m_offset));
m_offsetProperty.store(new FileOutputStream(m_writePath + "lastOffset.properties"), null);
}
}
正在创建和写入属性文件,但由于某种原因,我无法读取偏移键的值。此外,属性文件仅包含 1 个键/值对。为了简单起见,我取出了异常处理和许多其他方法。有任何想法吗?