如何检索 FileInputStream 的原始文件
我需要根据我使用的 API 来实现带有 InputStream
参数(FileInputStream
的实例)的方法,该方法将调用带有 File
的方法> 论证。
implementedMethod(InputStream is){
FileInputStream fis = (FileInputStream)is; //always works
File f = (???) ????(???).???;
calledMethod(f);
}
那么,当我只有 FileInputStream
时,如何提供该 File
呢?我不明白 FileChannel
或 FileDescriptor
(均在 FIS
中)到底是什么以及它们如何工作。
我猜测这是无法完成的,并且我必须在文件系统上实际写入一个(临时)文件。如果没有,如果我可以使用 FileChannel 或 FileDescriptor 在内存中创建文件,甚至使用 RandomAccessFile
(?)我想知道如何...
是否构建一个可读的 < code>File 对象要求它存在于磁盘上的某个位置?我觉得这很奇怪...
I need based on the APIs I use, to implement a method with an InputStream
parameter (instance of FileInputStream
) that will call a method with a File
argument.
implementedMethod(InputStream is){
FileInputStream fis = (FileInputStream)is; //always works
File f = (???) ????(???).???;
calledMethod(f);
}
So how do I provide that File
when all I have is a FileInputStream
? I don't understand what a FileChannel
or a FileDescriptor
(both in FIS
) are exactly and how they work.
I'm guessing that this can't be done and that I will have to actually write a (temp) file on the filesystem. If not, if I can create my file in memory instead with FileChannel or FileDescriptor, or even with something as RandomAccessFile
(?) I'd like to know how...
Is it that constructing a readable File
object requires it to exist somewhere on the disk? I find this odd...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,没有办法从 FileInputStream 获取文件名。
据我们所知,流可以由代表套接字的文件描述符构造。
我的建议是重新考虑你的设计。
As far as I know, there is no way to get the file name from a
FileInputStream
.For all we know, the stream could have been constructed from a file descriptor representing, for example, a socket.
My recommendation would be to rethink your design.
当然,您可以绕过 API,并假设它并不总是有效(取决于 JDK 和 SecurityManager),请使用反射:
You can of course bypass the API and, assuming it won't always work (depending on the JDK and the SecurityManager), use reflection: