Java InputStream 中的文件路径/名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2501936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:32:09  来源:igfitidea点击:

File path/name from InputStream

javaioinputstream

提问by Muhammad Hewedy

How to obtain File Path/Name from an InputStream in Java ?

如何从 Java 中的 InputStream 获取文件路径/名称?

采纳答案by b.roth

It's not possible.(not from the FileInputStream in the Java API). The FileInputStreamconstructor does not store this information in any field:

这是不可能的。(不是来自 Java API 中的 FileInputStream)。该FileInputStream构造函数不存储在任何领域这样的信息:

public FileInputStream(File file) throws FileNotFoundException {
    String name = (file != null ? file.getPath() : null);
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(name);
    }
        if (name == null) {
            throw new NullPointerException();
        }
    fd = new FileDescriptor();
    open(name);
    }

回答by Pyrolistical

You can't because the InputStreammight not have been a file or path. You can implement your own InputStreamthat generates data on the fly

你不能,因为它InputStream可能不是一个文件或路径。您可以实现自己的动态InputStream生成数据

回答by E-Riz

Two important Object Oriented Design principles prevent you from doing what you're asking: abstraction and encapsulation.

两个重要的面向对象设计原则阻止您做您所要求的事情:抽象和封装。

  • Abstractionis the process of defining a general concept that has only the details necessary for its use in a particular context (more details here). In this situation, the abstraction is InputStream, which is a general interface that can provide bytes, regardless of the source of those bytes. The abstraction of an InputStreamhas no concept of a file path; that's only relevant to particular implementations of InputStream.
  • Encapsulationis the process of hiding implementation details of a class from consumers/users of that class. In this particular situation, FileInputStreamencapsulates the details of the file it is reading from, because as an InputStreamthat information is not relevant to the usage. The pathinstance field is encapsulatedand as such unavailable to users of the class.
  • 抽象是定义一个一般概念的过程,该概念仅具有在特定上下文中使用所需的细节(更多细节在这里)。在这种情况下,抽象是InputStream,这是一个可以提供字节的通用接口,而不管这些字节的来源。an 的抽象InputStream没有文件路径的概念;这仅与InputStream.
  • 封装是对类的消费者/用户隐藏类的实现细节的过程。在这种特殊情况下,FileInputStream封装它正在读取的文件的详细信息,因为InputStream该信息与使用无关。该path实例字段被封装,因此无法使用这些类的用户。

Having said that, it ispossible to access the pathvariable if you are willing to accept some important limitations. Basically, the gist is that you can check if the InputStreamis, in fact, an instance of FileInputStreamand, if so, use reflection to read the pathinstance field. I'll leave out the details of doing that access since it's easily discoverablein the java.lang.ClassJava docs and online, and not really a generally good thing to do in most contexts. Since the question doesn't provide a context about why, it's hard to offer any more reasonable approaches.

话虽如此,如果您愿意接受一些重要的限制,可以访问该path变量。基本上,要点是您可以检查它是否InputStream实际上是一个实例FileInputStream,如果是,则使用反射来读取path实例字段。我将省略执行该访问的详细信息,因为它很容易在java.lang.ClassJava 文档和在线中找到,并且在大多数情况下通常不是一件好事。由于该问题没有提供有关原因的上下文,因此很难提供任何更合理的方法。