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:org.apache.hadoop.hdfs.server.common.Util.java

/**
 * Converts the passed File to a URI. This method trims the trailing slash if
 * one is appended because the underlying file is in fact a directory that
 * exists./*from  w  w  w  .  j a va2s. c  o  m*/
 *
 * @param f
 *     the file to convert
 * @return the resulting URI
 * @throws IOException
 */
public static URI fileAsURI(File f) throws IOException {
    URI u = f.getCanonicalFile().toURI();

    // trim the trailing slash, if it's present
    if (u.getPath().endsWith("/")) {
        String uriAsString = u.toString();
        try {
            u = new URI(uriAsString.substring(0, uriAsString.length() - 1));
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
    }

    return u;
}

From source file:com.indoqa.lang.util.FileUtils.java

/**
 * Gets the canonical form of the given <code>file</code>.
 * /*from   w  w  w  .j  a v a2s  .c o  m*/
 * If {@link File#getCanonicalFile()} fails, this method will return the given <code>file</code> instead of throwing an
 * {@link IOException}.
 * 
 * If the given <code>file</code> is NULL, this method will return NULL.
 * 
 * @param file The file to get the canonical form of.
 * @return The canonical file or if that fails, the given file.
 */
public static File getCanonicalFile(File file) {
    if (file == null) {
        return null;
    }

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

From source file:Utils.java

/**
 * Deletes a directory./*from  w  w  w .jav  a  2s .c o m*/
 *
 * @param dir the file for the directory to delete
 * @return true if susscess
 */
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                try {
                    if (file.getCanonicalFile().getParentFile().equals(dir.getCanonicalFile())) {
                        deleteDir(file);
                        if (file.exists() && !file.delete()) {
                            System.out.println("Can't delete: " + file);
                        }
                    } else {
                        System.out.println("Warning: " + file + " may be a symlink.  Ignoring.");
                    }
                } catch (IOException e) {
                    System.out.println("Warning: Cannot determine canonical file for " + file + " - ignoring.");
                }
            } else {
                if (file.exists() && !file.delete()) {
                    System.out.println("Can't delete: " + file);
                }
            }
        }
        return dir.delete();
    }
    return false;
}

From source file:org.opoo.util.PathUtils.java

public static File canonical(File file) {
    try {/* www  . ja  va  2 s  .co  m*/
        return file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

private static boolean isFileInsideDirectory(File child, File parent) throws IOException {

    child = child.getCanonicalFile();
    parent = parent.getCanonicalFile();/*  w ww. j a  v  a 2  s.  com*/

    while (child != null) {
        child = child.getParentFile();

        if (parent.equals(child))
            return true;
    }

    return false;

}

From source file:org.jruyi.cli.Main.java

private static void init() throws Throwable {
    ClassLoader classLoader = Main.class.getClassLoader();
    Method addUrl = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    boolean accessible = addUrl.isAccessible();
    if (!accessible)
        addUrl.setAccessible(true);/*from   www.  j ava  2s.c om*/

    File[] jars = getLibJars();
    for (File jar : jars)
        addUrl.invoke(classLoader, jar.getCanonicalFile().toURI().toURL());

    if (!accessible)
        addUrl.setAccessible(false);
}

From source file:com.jaeksoft.searchlib.util.FileUtils.java

public final static boolean isSubDirectory(File base, File child) throws IOException {
    base = base.getCanonicalFile();
    child = child.getCanonicalFile();//from  w  w  w  .jav  a2 s .  com
    File parent = child;
    while (parent != null) {
        if (base.equals(parent))
            return true;
        parent = parent.getParentFile();
    }
    return false;
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        if (!destFile.createNewFile()) {
            throw new IOException("Cannot create file: " + destFile.getCanonicalFile());
        }//w  ww. ja  v a2s.  c  o  m
    }
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        long count = 0;
        long size = source.size();
        while ((count += destination.transferFrom(source, count, size - count)) < size)
            ;
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:org.apache.flex.utils.FileUtils.java

public static File canonicalFile(File file) {
    try {/*from  w w w  .  ja v a 2 s  .c  om*/
        return file.getCanonicalFile();
    } catch (IOException e) {
        return file.getAbsoluteFile();
    }
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;//www . jav  a  2s  . c om
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}