我的基于java的webscript将存储库中的文件复制到临时文件夹中,并根据需要对其进行编辑。在其工作期间,会生成新内容,并且必须将其写入到创建的临时文件中。
但有一个问题:下面的第一个或第二个代码不会更新文件的内容。
ContentWriter contentWriter = this.contentService.getWriter(tempFile,
ContentModel.PROP_CONTENT, true);
contentWriter.putContent(content);第二个是:
`
WritableByteChannel byteChannel = contentWriter.getWritableChannel();
ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
byteChannel.write(buffer);
byteChannel.close();
`如何更新文件内容?
发布于 2011-10-13 18:02:23
这对我来说很有效:
ContentWriter contentWriter = contentService.getWriter(noderef, ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype("text/csv");
FileChannel fileChannel = contentWriter.getFileChannel(false);
ByteBuffer bf = ByteBuffer.wrap(logLine.getBytes());
try {
fileChannel.position(contentWriter.getSize());
fileChannel.write(bf);
fileChannel.force(false);
fileChannel.close();
} catch (IOException e){
e.printStackTrace();
}我将一行附加到现有文件中,因此logLine是附加字符串。
https://stackoverflow.com/questions/7752351
复制相似问题