Java Path Remove nio removeSymlink(Path softLinkLocation)

Here you can find the source of removeSymlink(Path softLinkLocation)

Description

remove Symlink

License

Open Source License

Declaration

public static void removeSymlink(Path softLinkLocation) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void removeSymlink(Path softLinkLocation) {
        try {/*from   w  w  w . j a  va2 s. c  o m*/
            BasicFileAttributes basicFileAttributes = getBasicFileAttributes(softLinkLocation);
            if (!basicFileAttributes.isSymbolicLink()) {
                throw new IOException("tried to delete a symlink which wasn't a symlink.");
            }
            Files.delete(softLinkLocation);
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    static BasicFileAttributes getBasicFileAttributes(Path path) {

        return getBasicFileAttributes(path, new LinkOption[] { LinkOption.NOFOLLOW_LINKS });
    }

    public static BasicFileAttributes getBasicFileAttributes(Path path, LinkOption[] options) {

        BasicFileAttributes basicFileAttributes;
        try {
            basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class, options);
        } catch (IOException e) {
            basicFileAttributes = null; // The file doesn't exist
        }
        return basicFileAttributes;
    }
}

Related

  1. removeFile(String path)
  2. removeFile(String workspacePath)
  3. removeLine(String path, String line, boolean commentAware)
  4. removeRecursive(Path path)
  5. removeRecursive(Path path)
  6. reportRemoveSymink(Path softLinkLocation)
  7. rm(final LinkedHashMap unremoved, Path... locations)