Creating a Path object and use its methods to further our understanding of the path. - Java File Path IO

Java examples for File Path IO:Path

Description

Creating a Path object and use its methods to further our understanding of the path.

Demo Code

import java.nio.file.FileSystems;
import java.nio.file.Path;

public class Main {

  public static void main(String[] args) {
    Path path = FileSystems.getDefault().getPath("/home/docs/status.txt");
    System.out.println();//from   w w w  .jav a 2s  .  c om
    System.out.printf("toString: %s\n", path.toString());
    System.out.printf("getFileName: %s\n", path.getFileName());
    System.out.printf("getRoot: %s\n", path.getRoot());
    System.out.printf("getNameCount: %d\n", path.getNameCount());
    for (int index = 0; index < path.getNameCount(); index++) {
      System.out.printf("getName(%d): %s\n", index, path.getName(index));
    }
    System.out.printf("subpath(0,2): %s\n", path.subpath(0, 2));
    System.out.printf("getParent: %s\n", path.getParent());
    System.out.println(path.isAbsolute());

    path = FileSystems.getDefault().getPath("\\home\\docs\\status.txt");
  }
}

Related Tutorials