java程序内存中的临时文件
有没有办法强制在内存中创建java程序的临时文件?由于我使用了几个大的xml文件,我这样会有优势吗?我是否应该使用一种透明的方法,让我不会扰乱现有的应用程序。
更新:我正在查看源代码,我注意到它使用库(我无法更改),这需要这些文件的路径......
谢谢
Is there a way to force the temporary files created in a java program in memory? Since I use several large xml file, I would have advantages in this way? Should I use a transparent method that allows me to not upset the existing application.
UPDATE: I'm looking at the source code and I noticed that it uses libraries (I can not change) which requires the path of those files ...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能想到的唯一方法是创建一个 RAM 磁盘,然后将系统属性 java.io.tmpdir 指向该 RAM 磁盘。
The only way I can think of is to create a RAM disk and then point the system property java.io.tmpdir to that RAM disk.
XML只是一个
String
,为什么不直接引用内存中的Strings
,我认为File接口是一种干扰。如果需要操作数据,请使用 StringBuilder。如果需要线程安全,请使用 StringBuffer。如果您需要使用键查找的内容数量不定,请将它们放入类型安全的Map
中。如果您绝对必须保留 File 接口,则创建一个包装 ByteArrayOutputStream 和 ByteArrayInputStream 的
InMemoryFileWriter
来保留它们内存中,但我再次认为,如果您只想将内容缓存在内存中,那么内存中的整个File
是一个错误的决定,当一个简单的String
时,这是很大的开销> 会做的。XML is just a
String
, why not just referenceStrings
in memory, I think the File interface is a distraction. UseStringBuilder
if you need to manipulate the data. UseStringBuffer
if you need thread safety. Put them in a type safeMap
if you have a variable number of things that need to be looked up on with a key.If you absolutely have to keep the
File
interface, then create aInMemoryFileWriter
that wrapsByteArrayOutputStream
andByteArrayInputStream
to keep them in memory, but again I think the wholeFile
in memory thing is a bad decision if you just want to cache things in memory, that is a lot of overhead when a simpleString
would do.如果不需要,请不要使用文件。考虑来自 Guava 的
com.google.common.io.FileBackedOutputStream
:Don't use files if you don't have to. Consider
com.google.common.io.FileBackedOutputStream
from Guava:您可能可以使用一些反射魔法强制 java.io.File 的默认行为,但我确信您不想这样做,因为它可能会导致不可预测的行为。您最好提供一种机制,可以在通常行为和内存行为之间切换,并通过此机制路由所有调用。
看看 这个例子,它展示了如何使用文件API创建内存文件。
You probably can force the default behaviour of
java.io.File
with some reflection magic, but I'm sure you don't want to do that as it can lead to unpredicted behaviour. You're better off providing a mechanism where it would be possible to switch between usual and in-memory behaviour, and route all calls via this mechanism.Look at this example, it shows how to use file API to create in-memory files.
假设您可以控制用于写入文件的流 -
您绝对想要内存中的行为吗?如果您只想减少写入磁盘的系统调用次数,则可以将 FileOutputStream 包装在 BufferedOutputStream (具有适当大的缓冲区大小)并写入此 BufferedOutputStream (或 BufferedWriter) 而不是直接写入原始 FileOutputStream。
(这确实需要对现有应用程序进行更改)
Assuming you have control over the the streams that are being used to write to the file -
Do you absolutely want the in-memory behavior? If all that you want to do is reduce the number of system calls to write to the disk, you can wrap the FileOutputStream in a BufferedOutputStream (with appropriately big buffer size) and write to this BufferedOutputStream (or BufferedWriter) instead of writing directly to the original FileOutputStream.
(This does require a change in the existing application)