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;//from www  . jav  a2  s.c  o  m
    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