Java I/O How to - Get the file name from the Path object








Question

We would like to know how to get the file name from the Path object.

Answer

import java.nio.file.FileSystems;
import java.nio.file.Path;
//www. j  a v a  2  s  .co  m
public class Main {

  public static void main(String[] args) {
    Path path = FileSystems.getDefault().getPath("/home/docs/status.txt");

    System.out.printf("getFileName: %s\n", path.getFileName());
  }
}

The code above generates the following result.