java程序内存中的临时文件

发布于 2024-10-17 20:35:28 字数 150 浏览 8 评论 0原文

有没有办法强制在内存中创建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

池予 2024-10-24 20:35:28

我能想到的唯一方法是创建一个 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.

臻嫒无言 2024-10-24 20:35:28

XML只是一个String,为什么不直接引用内存中的Strings,我认为File接口是一种干扰。如果需要操作数据,请使用 StringBuilder。如果需要线程安全,请使用 StringBuffer。如果您需要使用键查找的内容数量不定,请将它们放入类型安全的 Map 中。

如果您绝对必须保留 File 接口,则创建一个包装 ByteArrayOutputStream 和 ByteArrayInputStream 的 InMemoryFileWriter 来保留它们内存中,但我再次认为,如果您只想将内容缓存在内存中,那么内存中的整个 File 是一个错误的决定,当一个简单的 String 时,这是很大的开销> 会做的。

XML is just a String, why not just reference Strings in memory, I think the File interface is a distraction. Use StringBuilder if you need to manipulate the data. Use StringBuffer if you need thread safety. Put them in a type safe Map 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 a InMemoryFileWriter that wraps ByteArrayOutputStream and ByteArrayInputStream to keep them in memory, but again I think the whole File in memory thing is a bad decision if you just want to cache things in memory, that is a lot of overhead when a simple String would do.

飘然心甜 2024-10-24 20:35:28

如果不需要,请不要使用文件。考虑来自 Guava 的 com.google.common.io.FileBackedOutputStream

一个OutputStream,开始缓冲到字节数组,但一旦数据达到可配置的大小,就会切换到文件缓冲。

Don't use files if you don't have to. Consider com.google.common.io.FileBackedOutputStream from Guava:

An OutputStream that starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size.

风追烟花雨 2024-10-24 20:35:28

您可能可以使用一些反射魔法强制 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.

明月夜 2024-10-24 20:35:28

假设您可以控制用于写入文件的流 -

您绝对想要内存中的行为吗?如果您只想减少写入磁盘的系统调用次数,则可以将 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文