Java Path convert relative path to absolute path

Description

Java Path convert relative path to absolute path

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    String separator = FileSystems.getDefault().getSeparator();
    System.out.println("The separator is " + separator);
    try {//from  ww  w .  ja va  2 s  .  com
      Path path = Paths.get(new URI("file:///C:/home/docs/users.txt"));
      
      System.out.println("subpath: " + path.subpath(0, 3));
      path = Paths.get("/home", "docs", "users.txt");
    
      System.out.println("Absolute path: " + path.toAbsolutePath());
      System.out.println("URI: " + path.toUri());

    } catch (URISyntaxException ex) {
      System.out.println("Bad URI");
    } catch (InvalidPathException ex) {
      System.out.println("Bad path: [" + ex.getInput() + "] at position " + ex.getIndex());
    }
  }
}



PreviousNext

Related