Created
January 11, 2017 18:58
-
-
Save StefRe/ba66b304e54a0b672ff31d3fd76b21a1 to your computer and use it in GitHub Desktop.
Java File and Path methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| public class FileDemo { | |
| public static void main(String[] args) throws Exception { | |
| if( args.length != 1 ){ | |
| System.err.println( "Usage: FileDemo file" ); | |
| System.exit(1); | |
| } | |
| System.out.println( "*** Argument *****: " + args[0] ); | |
| System.out.println(); | |
| File f = new File(args[0]); | |
| System.out.println( "--- File ---------" ); | |
| System.out.println( "toString: " + f.toString() ); | |
| System.out.println( "getName: " + f.getName() ); | |
| System.out.println( "getAbsolutePath: " + f.getAbsolutePath() ); | |
| System.out.println( "getCanonicalPath: " + f.getCanonicalPath() ); | |
| System.out.println( "getParent: " + f.getParent() ); | |
| System.out.println( "getPath: " + f.getPath() ); | |
| System.out.println( "getCanonicalFile().getParent(): " + f.getCanonicalFile().getParent() ); | |
| System.out.println(); | |
| Path p = Paths.get(args[0]); | |
| System.out.println( "--- Path ---------" ); | |
| System.out.println( "toString: " + p.toString() ); | |
| System.out.println( "getFileName: " + p.getFileName() ); | |
| System.out.println( "toAbsolutePath: " + p.toAbsolutePath() ); | |
| System.out.println( "normalize: " + p.normalize() ); | |
| System.out.println( "getParent: " + p.getParent() ); | |
| System.out.println( "getRoot: " + p.getRoot() ); | |
| System.out.println( "getNameCount: " + p.getNameCount() ); | |
| for( int i = 0; i < p.getNameCount(); i++ ) | |
| System.out.println( " getName(" + i + ") " + p.getName(i) ); | |
| System.out.println( "resolve(\"foo\"): " + p.resolve("foo") ); | |
| System.out.println( "resolveSibling(\"foo\"): " + p.resolveSibling("foo") ); | |
| System.out.println(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: