How to obtain File Path/Name from an InputStream in Java ?
is it possible ? I'm afraid I can't follow precisely what you're asking, but: there's no way to get the filename that a FileOutputStream or FileInputStream is connected to, even within a single JVM; it's doubly impossible in a client/server situation.
int read() − This simply reads data from the current InputStream and returns the read data byte by byte (in integer format). This method returns -1 if the end of the file is reached. int read(byte[] b) − This method accepts a byte array as parameter and reads the contents of the current InputStream, to the given array.
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .
Returns. the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
It's not possible. (not from the FileInputStream in the Java API). The FileInputStream
constructor does not store this information in any field:
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);
}
You can't because the InputStream
might not have been a file or path. You can implement your own InputStream
that generates data on the fly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With