Example usage for java.io File getCanonicalFile

List of usage examples for java.io File getCanonicalFile

Introduction

In this page you can find the example usage for java.io File getCanonicalFile.

Prototype

public File getCanonicalFile() throws IOException 

Source Link

Document

Returns the canonical form of this abstract pathname.

Usage

From source file:com.apporiented.hermesftp.utils.IOUtils.java

/**
 * Tries to figure out the application's home directory.
 * //from w ww  .  jav a2  s .  co m
 * @return The directory.
 */
public static File getHomeDir() {
    File result = null;
    String cp = System.getProperty("java.class.path");
    StringTokenizer tokenizer = new StringTokenizer(cp, File.pathSeparator);
    if (tokenizer.countTokens() == 1) {
        File jar = new File(tokenizer.nextToken());
        try {
            result = jar.getCanonicalFile().getParentFile().getParentFile();
        } catch (IOException e) {
        }
    } else {
        File userDir = new File(System.getProperty("user.dir"));
        result = userDir.getAbsoluteFile().getParentFile();
    }
    return result;
}

From source file:Main.java

public static File getRelativeFile(File baseFile, File fileToRelativize) throws IOException {
    if (baseFile.isFile()) {
        baseFile = baseFile.getParentFile();
    }/* w ww  .  jav  a2  s . co  m*/

    return new File(getRelativeFileInternal(baseFile.getCanonicalFile(), fileToRelativize.getCanonicalFile()));
}

From source file:com.apporiented.hermesftp.PluginManager.java

/**
 * Get the directory where plugins are installed.
 * // w  ww.  j  a  va 2 s  .com
 * @return the directory where plugins are installed.
 */
public static File getPluginDir() {
    String dir = System.getProperty(FtpConstants.HERMES_HOME);
    if (StringUtils.isEmpty(dir)) {
        dir = System.getProperty("user.dir");
    }
    File file = new File(dir, "plugins");
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        log.debug("No canonical path: " + file);
    }
    return file;
}

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {/*from  w w w. j a  va  2 s . c o  m*/
    final String curDir = System.getProperty("user.dir");
    JFileChooser chooser = new JFileChooser(curDir);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setFileSelectionMode(selectionMode);
    chooser.setApproveButtonText("Select");
    chooser.setApproveButtonMnemonic('s');
    chooser.setDialogTitle(title);
    if (filter != null)
        chooser.setFileFilter(filter);

    if (file != null && !file.isEmpty()) {
        File curFile = new File(file);

        chooser.setCurrentDirectory(curFile.getAbsoluteFile().getParentFile());

        if (curFile.isDirectory()) {
            try {
                chooser.setSelectedFile(curFile.getCanonicalFile());
            } catch (IOException ex) {
            }
        } else {
            chooser.setSelectedFile(curFile);
        }
    }

    if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getPath();
        try {
            path = new File(path).getCanonicalPath();
        } catch (IOException e) {
        }
        // make path relative if possible
        if (path.startsWith(curDir)) {
            path = "." + path.substring(curDir.length());
        }

        return path;
    }
    return null;
}

From source file:org.apereo.portal.shell.PortalShell.java

protected static File getAbsoluteFile(final String filePath) {
    final File file;
    if (FileUtils.isAbsolutePath(filePath)) {
        file = new File(filePath);
    } else {//from  w w  w .  j a  v a2  s. c  om
        file = new File(new File(System.getProperty("user.dir")), filePath);
    }

    try {
        return file.getCanonicalFile();
    } catch (IOException e) {
        return file;
    }
}

From source file:Main.java

public static boolean isSymLink(File filePath) throws IOException {
    if (filePath == null)
        throw new NullPointerException("filePath cannot be null");
    File canonical;
    if (filePath.getParent() == null) {
        canonical = filePath;/*w w  w .ja  va 2s.co m*/
    } else {
        File canonDir = filePath.getParentFile().getCanonicalFile();
        canonical = new File(canonDir, filePath.getName());
    }
    return !canonical.getCanonicalFile().equals(canonical.getAbsoluteFile());
}

From source file:org.apache.hadoop.util.DiskChecker.java

/** 
 * The semantics of mkdirsWithExistsCheck method is different from the mkdirs
 * method provided in the Sun's java.io.File class in the following way:
 * While creating the non-existent parent directories, this method checks for
 * the existence of those directories if the mkdir fails at any point (since
 * that directory might have just been created by some other process).
 * If both mkdir() and the exists() check fails for any seemingly 
 * non-existent directory, then we signal an error; Sun's mkdir would signal
 * an error (return false) if a directory it is attempting to create already
 * exists or the mkdir fails./* www  . j a  va  2  s  .  co m*/
 * @param dir
 * @return true on success, false on failure
 */
public static boolean mkdirsWithExistsCheck(File dir) {
    if (dir.mkdir() || dir.exists()) {
        return true;
    }
    File canonDir = null;
    try {
        canonDir = dir.getCanonicalFile();
    } catch (IOException e) {
        return false;
    }
    String parent = canonDir.getParent();
    return (parent != null)
            && (mkdirsWithExistsCheck(new File(parent)) && (canonDir.mkdir() || canonDir.exists()));
}

From source file:psidev.psi.mi.filemakers.xsd.Utils.java

/**
 * break a path down into individual elements and add to a list. example :
 * if a path is /a/b/c/d.txt, the breakdown will be [d.txt,c,b,a]
 * /*from w w  w . ja  va2  s  . c  o m*/
 * @param f
 *            input file
 * @return a List collection with the individual elements of the path in
 *         reverse order
 */
private static List<String> getPathList(File f) {
    List<String> l = new ArrayList<String>();
    File r;
    try {
        r = f.getCanonicalFile();
        while (r != null) {
            l.add(r.getName());
            r = r.getParentFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
        l = null;
    }
    return l;
}

From source file:org.rapidcontext.util.FileUtil.java

/**
 * Attempts to resolve a file to a canonical file.
 *
 * @param file           the file to resolve
 *
 * @return the canonical file, or//from w  w w.ja  v  a 2 s  .c  o  m
 *         the original file if the resolution failed
 */
public static File canonical(File file) {
    try {
        return file.getCanonicalFile();
    } catch (IOException ignore) {
        return file;
    }
}

From source file:org.apache.openejb.config.Undeploy.java

public static void undeploy(String moduleId, File file, final Deployer deployer)
        throws UndeployException, NoSuchApplicationException, DeploymentTerminatedException {
    try {// w w w  .j  a  v a 2  s .c  o m
        file = file.getCanonicalFile();
    } catch (final IOException e) {
        // no-op
    }

    boolean undeployed = false;
    if (file != null) {
        final String path = file.getAbsolutePath();
        try {
            deployer.undeploy(path);
            undeployed = true;
            moduleId = path;
            if (!delete(file)) {
                throw new DeploymentTerminatedException(
                        messages.format("cmd.undeploy.cantDelete", file.getAbsolutePath()));
            }
        } catch (final NoSuchApplicationException e) {
            // no-op
        }
    }

    // If that didn't work, undeploy using just the moduleId
    if (!undeployed) {
        deployer.undeploy(moduleId);
        System.out.println(messages.format("cmd.undeploy.nothingToDelete", moduleId));
    }

    // TODO make this message
    System.out.println(messages.format("cmd.undeploy.successful", moduleId));
}