可以创建一个 java RAM 磁盘以与 java.io.* API 一起使用吗? [英] Can a java RAM disk be created to be used with the java.io.* API?

查看:22
本文介绍了可以创建一个 java RAM 磁盘以与 java.io.* API 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 3rd 方库,它基本上创建了一个包含不同类型文件和子目录的输出目录.我希望能够编写单元测试以确认输出正确.

I'm using a 3rd party library which basically creates an output directory with different kinds of files and subdirectories inside. I would like to be able to write unit tests to confirm that the output is correct.

我希望能够将库与 RAM 磁盘一起使用,这样库所做的任何事情都不会以任何方式接触实际的磁盘板.这个想法是让测试运行和清理速度非常快(删除 RAM 磁盘?).

I would like to be able to use the lib with a RAM disk, so that nothing the library does touches actual disk plates in any way. The idea is to make the tests very fast to run and clean up (drop RAM disk?).

我可以使用的两个最突出的选项是 Commons VFSJSR 203.前者对我没有用,因为我希望使用 java.io.* API 而不是 Commons VFS 类透明地工作.后者并没有削减它,因为我必须使用 JDK 6(它应该是 JDK 7 的一部分)而且我不知道它是否可以与 java.io.* 无缝地工作(我不会赌它).

The two most prominent options available to me are Commons VFS and JSR 203. The former is of no use to me because I want things to work transparently using the java.io.* API and not Commons VFS classes. The later doesn't cut it because I have to make do with JDK 6 (it's supposed to be a part of JDK 7) and I don't know if it will work seamlessly with java.io.* anyway (I wouldn't bet on it).

还有其他解决方案,但我不能将它们用于同样的原因我不能使用 Commons VFS.由于相关库的复杂性,模拟是不可能的.

There are other solutions as well, but I can't use them for the same reason I can't use Commons VFS. Mocks are out of the question because of the complexity of the library in question.

在我的 linux 机器上,我可以轻松地创建一个 RAM 驱动器并使用 java.io.* API,就像处理磁盘上的文件一样.问题是,我希望它是跨平台的,更具体地说,让磁盘设置成为测试过程的一部分,而不是外部的东西.

On my linux machine, I can easily create a RAM drive and use the java.io.* API the same way I would with files on disk. The thing is, I want it to be cross-platform and more specifically, to make disk setup a part of the test procedure, rather than something external.

那么,有没有办法在 Java 中注册一个 RAM 驱动器,它可以与标准的 java.io.* API 一起使用?

So, is there a way to register a RAM drive in Java which would be usable with the standard java.io.* API?

推荐答案

那么,有没有办法在 Java 中注册一个 RAM 驱动器,它可以与标准的 java.io.* API 一起使用?

So, is there a way to register a RAM drive in Java which would be usable with the standard java.io.* API?

不适用于 Java 6 或更早版本的 JVM.Java 6 及更早版本不提供任何用于注册文件系统或文件系统类型的 SPI.因此,要实现应用程序可以像普通 FS 一样使用的 RAM FS,需要修改许多 java.io.* 类的行为.

Not with a Java 6 or earlier JVM. Java 6 and earlier do not provide any SPI for registering file systems or file system types. So, to implement a RAM FS that an application would use like a normal FS would entail modifying the behavior of a number of java.io.* classes.

我认为您能做的最好的事情是使用由主机操作系统实现的 RAM FS.您应该能够像访问普通文件系统一样从 Java 访问它.但是,I/O 需要系统调用,因此它不会像 RAM 文件系统保存在 JVM 管理的内存中那样快.

I think that the best thing that you could do would be to use a RAM FS implemented by the host operating system. You should be able to access that from Java as if it was a normal file system. However, I/O would entail a system calls, so it wouldn't be as fast as if the RAM file system was held in the JVM managed memory.

这篇关于可以创建一个 java RAM 磁盘以与 java.io.* API 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆