Last active
January 5, 2017 16:18
-
-
Save peterkir/1df3f0bf8508d8836101 to your computer and use it in GitHub Desktop.
TestJunctionWithJavaNIO
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
package test; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.List; | |
/* | |
* | |
* prepare the directory from cmd with admin rights | |
* | |
* mkdir c:\temp\a | |
* cd /d c:\temp | |
* mklink /j junction_to_a a | |
* mklink /d symlink_to_a a | |
* | |
* Hardlinks only on files -> http://msdn.microsoft.com/en-us/library/aa365006%28VS.85%29.aspx | |
* echo 1 > b | |
* mklink /h hardlink_to_b b | |
* | |
*/ | |
public class TestJunction { | |
/* | |
* results on my machine | |
* | |
* # test for junction | |
* a -> isJunction = false | |
* junction_to_a -> isJunction = true | |
* symlink_to_a -> isJunction = true | |
* hardlink_to_b -> isJunction = false | |
* | |
* # test for symlink | |
* a -> isSymbolicLink = false | |
* junction_to_a -> isSymbolicLink = false | |
* symlink_to_a -> isSymbolicLink = true | |
* hardlink_to_b -> isSymbolicLink = false | |
* | |
* # test for regular file | |
* a -> isRegularFile = false | |
* junction_to_a -> isRegularFile = false | |
* symlink_to_a -> isRegularFile = false | |
* hardlink_to_b -> isRegularFile = true | |
*/ | |
public static void main(String[] args) { | |
String dir = "c:/temp"; | |
List<String> fileNames = Arrays.asList("a","junction_to_a","symlink_to_a", "hardlink_to_b"); | |
System.out.println("\n# test for junction"); | |
fileNames.forEach(file -> { | |
System.out.format("%15s -> isJunction = %s\n", file, isJunction(Paths.get(dir, file))); | |
}); | |
System.out.println("\n# test for symlink"); | |
fileNames.forEach(file -> { | |
System.out.format("%15s -> isSymbolicLink = %s\n", file, Files.isSymbolicLink(Paths.get(dir, file))); | |
}); | |
System.out.println("\n# test for regular file"); | |
fileNames.forEach(file -> { | |
System.out.format("%15s -> isRegularFile = %s\n", file, Files.isRegularFile(Paths.get(dir, file))); | |
}); | |
} | |
/** | |
* returns true if the Path is a Windows Junction | |
*/ | |
private static boolean isJunction(Path p) { | |
boolean isJunction = false; | |
try { | |
isJunction = (p.compareTo(p.toRealPath()) != 0); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); // TODO: handleMeProperly | |
} | |
return isJunction; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment