特殊问题是当我使用从文件路径获得的properties.store
代码时FileOutputStream
,上述方法工作正常,但是当我FileOutputStream
从FileDescriptor
属性文件追加到它时,不会覆盖。
现在我的限制是使用后一种方法,因为我正在使用 FileLock 并且无法再次通过文件获取 FileOutputStream。
- 可能吗?用后一种方法做某事并覆盖和
- 如果不是,我有什么选择?
两段代码是
第一种方法
OutputStream out = null;
try {
if (portFile != null && portFile.exists()) {
out = new FileOutputStream(portFile);
} else {
try {
portFile.createNewFile();
} catch (IOException e) {
LOGGER.error("error in creating properties file ", e);
}
out = new FileOutputStream(portFile);
}
} catch (FileNotFoundException e) {
LOGGER.error("Not able to get outputstream on properties file ", e);
}
try {
properties.store(out, CJDPluginConstants.PROP_NAME_PORT_MIN);
} catch (IOException e) {
LOGGER.error("Not able to save properties file ", e);
}
第二种方法
// so we move to raf now
OutputStream out = null;
if (portFileRandomAccess != null && channel != null) {
//ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
if (buffer != null) {
//if (buffer != null) {
buffer.flip();
LOGGER.info("buffer what we get we are writing ? " + buffer.asCharBuffer());
out = new ByteBufferBackedOutputStream(buffer);
FileOutputStream fos = new FileOutputStream(portFileRandomAccess.getFD());
//fos.flush();
properties.store(fos, CJDPluginConstants.PROP_NAME_PORT_MIN);
buffer.clear();
buffer = null;
//}
}
} catch (IOException e) {
LOGGER.error("Not able to save properties file ", e);
}
}
//release lock, close channel
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
LOGGER.error("Error releasing lock", e);
}
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
LOGGER.error("Error closing channel", e);
}
}