[Solved] How to get the file name from the directory using InputStream in Java [closed]


First of all, what you are doing is fragile:

  • If the InputStream was not a FileInputStream it would fail.
  • If a future Java releases changes the internals of FileInputStream, it may fail.
  • It is likely to fail if your code is sandboxed.

It would be much better to keep / pass the value of the parameter you used when instantiating the FileInputStream.

Having said that, to get just the filename, you need to use a File or Path to extract it; e.g.

  String justTheFileName = new File(fileName).getName(); 

or

  String justTheFileName = Paths.get(fileName).getFileName();

solved How to get the file name from the directory using InputStream in Java [closed]