Jonah Sloan Jonah Sloan - 10 months ago 58
Java Question

Getting the file used by a FileInputStream

Is it possible to obtain the

being used by a
does not appear to have any methods for retrieving it.

Answer Source

There are no direct methods in FileInputStream API, but if you really wanted, you can use java reflection API to get the path (actual file name with full path) as shown below:

FileInputStream fis = new FileInputStream(inputFile);
Field field = fis.getClass().getDeclaredField("path");
String path  = (String)field.get(fis);

The path variable (holds the file name with path) is declared in the FileInputStream class as a private final field, which we are getting it using reflections code as shown above.