java相当于.net FileStream
我有一个小的.Net 库,我想将其移植到java。该库的部分要求是它应该将状态序列化到磁盘上的文件中,并且该文件应保持“打开”状态,以便该库的其他实例无法访问该文件,但大多数实例都会创建自己的实例。在 C# 中,我使用使用 FileShare.None 打开的 FileStream 来防止其他人读取/写入文件。当持有 FileStream 时,我可以读取和写入文件,调用 Flush() 写入磁盘并使用 SetLength(..) 我可以在写入内容后缩短已经存在的文件。
在 java 中,我不确定哪个类提供最相似的属性。 RandomAccessFile 是我最好的选择,但由于我不太精通 java,我可能会忽略一些更适合的类或 RandomAccessFile 的一些警告。
I have a small .Net library that I would like to port to java. Part of the requirement for this library is that it should serialize state into a file on disk and this file should remain "open" so other instances of the library cannot access this file but most create their own instance. In C# I have used the FileStream which is opened with FileShare.None to guard against others reading/writing to the file. When holding the FileStream I can read and write to the file, calling Flush() to write to disk and using SetLength(..) I can shorten an already existing file after write content.
In java I am not certain which class offers the most similar properties. The RandomAccessFile is my best bet but since I am not exactly well-versed in java I might be overlooking some better suited class or some caveat with RandomAccessFile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 FileWriter,阅读 API 文档,看看它是否适合您的需求。
再次阅读您的帖子,我实际上认为这可能会回答您的问题:
如何防止文件在使用Java读取和处理时被覆盖?
Check out FileWriter, read the API-Docs to see whether it suits your needs.
Reading your post again, i actually think this might answer your question:
How to prevent file from being overridden when reading and processing it with Java?