Java Utililty Methods File Link

List of utility methods to do File Link

Description

The list of methods to do File Link are organized into topic(s).

Method

voidaddSymlinkToFileInsideWorkspace()
add Symlink To File Inside Workspace
Files.createSymbolicLink(Paths.get(TEST_DIR, "SymlinkFile"), Paths.get("Dir1", "out.txt"));
FilecreateRelativeSymlink(File link, File target, boolean relativize)
create Relative Symlink
if (relativize) {
    link = link.getCanonicalFile();
    target = target.getCanonicalFile();
    try {
        target = link.getParentFile().toPath().relativize(target.toPath()).toFile();
    } catch (Throwable e) {
return Files.createSymbolicLink(link.toPath(), target.toPath()).toFile();
FilecreateSymbolicLink(@Nonnull File symlink, File target)
create Symbolic Link
Path link = symlink.toPath();
if (!Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
    link = java.nio.file.Files.createSymbolicLink(link, target.toPath());
return link.toFile();
voidcreateSymbolicLink(File pTargetFile, File pLink)
Wrapper for creating a symbolic link using java.nio
Path target = Paths.get(pTargetFile.toURI());
Path link = Paths.get(pLink.toURI());
Files.createSymbolicLink(link, target);
booleanfollowLinks(LinkOption... options)
Returns true if symbolic links should be followed
boolean followLinks = true;
for (LinkOption option : options) {
    if (option == LinkOption.NOFOLLOW_LINKS) {
        followLinks = false;
    } else if (option == null) {
        throw new NullPointerException();
    } else {
        throw new AssertionError("Should not get here");
...
LinkOption[]getLinkOptions(boolean followLinks)
get Link Options
if (followLinks) {
    return EMPTY_LINK_OPTIONS;
} else { 
    return NO_FOLLOW_OPTIONS.clone();
booleanisLink(File f)
is Link
return Files.isSymbolicLink(Paths.get(f.getPath()));
booleanisSymbolicLink(File file)
is Symbolic Link
try {
    File canonicalFile = file.getCanonicalFile();
    File absoluteFile = file.getAbsoluteFile();
    return !canonicalFile.getName().equals(absoluteFile.getName()) ||
            !canonicalFile.getParent().equals(absoluteFile.getParentFile().getCanonicalPath());
} catch (IOException e) {
    return true;
booleanisSymbolicLink(String file)
returns true for symbolic link , also "soft" link for windows, it is creates using mklink /D Link Target uses rmdir to remove soft links on windows.
Path path = Paths.get(file);
boolean isSymbolicLink = Files.isSymbolicLink(path);
return isSymbolicLink;
booleanisSymlink(File file)
is Symlink
return Files.isSymbolicLink(file.toPath());