Java examples for File Path IO:Path
Converting a relative path into an 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 w w w . jav a 2 s. c o m 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()); } } }