public static Path get(String first,
String… more)
Converts a path string, or a sequence of strings that when joined form a path string, to a Path. If more does not specify any elements then the value of the first parameter is the path string to convert. If more specifies one or more elements then each non-empty string, including first, is considered to be a sequence of name elements (see Path) and is joined to form a path string. The details as to how the Strings are joined is provider specific but typically they will be joined using the name-separator as the separator. For example, if the name separator is “/” and getPath(“/foo”,”bar”,”gus”) is invoked, then the path string “/foo/bar/gus” is converted to a Path. A Path representing an empty path is returned if first is the empty string and more does not contain any non-empty strings.
The Path is obtained by invoking the getPath method of the default FileSystem.
Note that while this method is very convenient, using it will imply an assumed reference to the default FileSystem and limit the utility of the calling code. Hence it should not be used in library code intended for flexible reuse. A more flexible alternative is to use an existing Path instance as an anchor, such as:
Path dir = …
Path path = dir.resolve(“file”);
Parameters:
first – the path string or initial part of the path string
more – additional strings to be joined to form the path string
Returns:
the resulting Path
Throws:
InvalidPathException – if the path string cannot be converted to a Path
package com.candidjava.filenio; import java.nio.file.Path; import java.nio.file.Paths; public class FilePath { public static void main(String[] args) { Path relative=Paths.get("move.txt"); System.out.println("Relative Path:"+relative); Path absolute=relative.toAbsolutePath(); System.out.println("Absolute Path:"+absolute); } }
Old Approach
package com.candidjava.file; import java.io.File; public class FilePath { public static void main(String[] args) { File file = new File("../inputfile.txt"); try { String originalpath = file.getPath(); String abspath = file.getAbsolutePath(); String canonicalpath = file.getCanonicalPath(); System.out.println("original Path:" + originalpath); System.out.println("Absolute Path:" + abspath); System.out.println("Canonical Path:" + canonicalpath); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }