How to obtain File Path/Name from an InputStream in Java ?
Two important Object Oriented Design principles prevent you from doing what you're asking: abstraction and encapsulation.
InputStream
, which is a general interface that can provide bytes, regardless of the source of those bytes. The abstraction of an InputStream
has no concept of a file path; that's only relevant to particular implementations of InputStream
.FileInputStream
encapsulates the details of the file it is reading from, because as an InputStream
that information is not relevant to the usage. The path
instance field is encapsulated and as such unavailable to users of the class.Having said that, it is possible to access the path
variable if you are willing to accept some important limitations. Basically, the gist is that you can check if the InputStream
is, in fact, an instance of FileInputStream
and, if so, use reflection to read the path
instance field. I'll leave out the details of doing that access since it's easily discoverablein the java.lang.Class
Java 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.