Creating a path between two locations - Java File Path IO

Java examples for File Path IO:Path

Description

Creating a path between two locations

Demo Code

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path firstPath;/*  w  w w. j  av a 2  s . c om*/
    Path secondPath;
    firstPath = Paths.get("music/Future Setting A.mp3");
    secondPath = Paths.get("docs");
    System.out.println("From firstPath to secondPath: "
        + firstPath.relativize(secondPath));
    System.out.println("From secondPath to firstPath: "
        + secondPath.relativize(firstPath));
    System.out.println();

    firstPath = Paths.get("music/Future Setting A.mp3");
    secondPath = Paths.get("music");
    System.out.println("From firstPath to secondPath: "
        + firstPath.relativize(secondPath));
    System.out.println("From secondPath to firstPath: "
        + secondPath.relativize(firstPath));
    System.out.println();

    firstPath = Paths.get("music/Future Setting A.mp3");
    secondPath = Paths.get("docs/users.txt");
    System.out.println("From firstPath to secondPath: "
        + firstPath.relativize(secondPath));
    System.out.println("From secondPath to firstPath: "
        + secondPath.relativize(firstPath));
    System.out.println();
  }

}

Related Tutorials