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.github.jengelman.gradle.plugins.integration.TestFile.java

private static File join(File file, Object[] path) {
    File current = file.getAbsoluteFile();
    for (Object p : path) {
        current = new File(current, p.toString());
    }/*from w w w .j a  v a  2  s  .co m*/
    try {
        return current.getCanonicalFile();
    } catch (IOException e) {
        throw new RuntimeException(String.format("Could not canonicalise '%s'.", current), e);
    }
}

From source file:org.normandra.CassandraTestUtil.java

public static void start(final String yamlFile) throws Exception {
    if (embedded != null) {
        return;/*from  ww w  . j  a v a2  s  .  co m*/
    }

    log4j(false);

    final File embeddedDir = new File("target/embeddedCassandra").getCanonicalFile();
    final File tmpYaml = new File(embeddedDir, FilenameUtils.getName(yamlFile));
    if (tmpYaml.exists()) {
        FileUtils.forceDelete(tmpYaml);
    }
    if (embeddedDir.exists()) {
        FileUtils.deleteDirectory(embeddedDir);
    }
    FileUtils.copyURLToFile(CassandraTestUtil.class.getResource(yamlFile), tmpYaml);

    System.setProperty("cassandra.config", tmpYaml.getCanonicalFile().toURI().toString());
    System.setProperty("cassandra-foreground", "true");

    clearAndReset();

    if (null == embedded) {
        final AtomicBoolean started = new AtomicBoolean(false);
        embedded = new EmbeddedCassandraService();
        final Runnable worker = new Runnable() {
            @Override
            public void run() {
                try {
                    embedded.start();
                    started.getAndSet(true);
                } catch (final Exception e) {
                    logger.warn("Unable to start embedded cassandra server.", e);
                }
            }
        };
        final Thread thread = new Thread(worker);
        thread.setDaemon(true);
        thread.setName(CassandraTestUtil.class.getSimpleName() + "[embedded]");
        thread.start();

        for (int i = 0; i < 60; i++) {
            if (started.get()) {
                break;
            }
            Thread.sleep(250);
        }
    }
}

From source file:net.landora.video.info.file.FileHasher.java

public static void checkDirectory(File dir) throws IOException {
    for (File file : dir.listFiles()) {
        if (file.isHidden()) {
            continue;
        }//from   w  w w . jav a 2  s .  co  m

        if (file.isDirectory()) {
            checkDirectory(file);
        } else {

            String extension = ExtensionUtils.getExtension(file);
            if (!ExtensionUtils.isVideoExtension(extension)) {
                continue;
            }

            FileInfo info = FileInfoManager.getInstance().getFileInfo(file);
            VideoMetadata md = MetadataProvidersManager.getInstance().getMetadata(info);

            if (md == null) {
                continue;
            }

            file = file.getCanonicalFile();

            String filename = getOutputFilename(md) + "." + extension;

            File outputFile = new File(getOutputFolder(md), filename);
            outputFile.getParentFile().mkdirs();
            outputFile = outputFile.getCanonicalFile();

            if (!outputFile.equals(file)) {
                if (outputFile.exists()) {
                } else {
                    FileUtils.moveFile(file, outputFile);
                }

            }

        }
    }
}

From source file:org.limewire.util.FileUtils.java

/** Same as f.getCanonicalFile() in JDK1.3. */
public static File getCanonicalFile(File f) throws IOException {
    try {//  ww  w . j ava  2s.c o m
        return f.getCanonicalFile();
    } catch (IOException ioe) {
        String msg = ioe.getMessage();
        // windows bugs out :(
        if (OSUtils.isWindows() && msg != null && msg.indexOf("There are no more files") != -1)
            return f.getAbsoluteFile();
        else
            throw ioe;
    }
}

From source file:com.bc.util.io.FileUtils.java

/**
 * @param dereferenceSymbolicLinks means that when a link is found and points is a directory, we go in that directory
 * and delete the contents there, even if the contents are not inside the treeRoot
 *///  w w  w.  java  2 s.c  o m
public static void deleteFileTree(File treeRoot, boolean dereferenceSymbolicLinks) throws IOException {
    if (isSymbolicLink(treeRoot)) {
        treeRoot.delete();
        return;//do not continue... if you do, the listFiles() method might return files outside the treeRoot
    }

    File[] files = treeRoot.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            boolean linkFound = isSymbolicLink(file);
            if (!dereferenceSymbolicLinks && linkFound) {
                // if it is a link, and we are not supposed to dereference symlinks
                file.delete(); //delete the symbolic link
            } else {
                if (file.isDirectory()) {
                    // if it is a directory, recursive call
                    if (linkFound) {
                        File canonicalFile = file.getCanonicalFile();
                        if (!canonicalFile.exists() || directoryContainsFile(treeRoot, canonicalFile)) {
                            //if canonicalFile doesn't exist, or is inside treeRoot, skip dereferencing and just delete the link
                            file.delete();//delete the symbolic link
                        } else {
                            //delete linked directory and its contents
                            deleteFileTree(canonicalFile, dereferenceSymbolicLinks);
                            file.delete();//delete the symbolic link
                        }
                    } else {
                        deleteFileTree(file, dereferenceSymbolicLinks);
                    }
                } else {
                    // if it is a normal file, delete it
                    file.delete();
                }
            }
        }
    }

    treeRoot.delete();
}

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

private static File[] getLibJars() throws Throwable {
    File homeDir;
    String temp = System.getProperty("jruyi.home.dir");
    if (temp == null) {
        String classpath = System.getProperty("java.class.path");
        int index = classpath.toLowerCase().indexOf("jruyi-cli");
        int start = classpath.lastIndexOf(File.pathSeparator, index) + 1;
        if (index >= start) {
            temp = classpath.substring(start, index);
            homeDir = new File(temp).getCanonicalFile().getParentFile();
        } else/*from  w  w w.  j  av  a  2  s  . c  o  m*/
            // use current dir
            homeDir = new File(System.getProperty("user.dir"));
    } else
        homeDir = new File(temp);

    homeDir = homeDir.getCanonicalFile();
    return new File(homeDir, "lib").listFiles(new JarFileFilter());
}

From source file:Main.java

/**
 * Resolves the relative path of the specified artifact
 *
 * @param outputDirectory typically the parent directory of the directory containing the makefile
 * @param file/* www.j a v a2s . com*/
 * @return
 */
protected static String resolveRelativePath(File outputDirectory, File file) throws IOException {
    String resolvedPath = file.getCanonicalPath();

    String strOutputDirectoryPath = outputDirectory.getCanonicalPath();
    String strFilePath = file.getCanonicalPath();
    //System.out.println( "Resolving " + strFilePath + " against " + strOutputDirectoryPath );

    if (strFilePath.startsWith(strOutputDirectoryPath)) {
        // Simple case where file is in a subdirectory of outputDirectory
        resolvedPath = strFilePath.substring(strOutputDirectoryPath.length() + 1);
    } else {
        // Look for commonality in paths
        List<String> outputDirectoryPathParts = splitPath(outputDirectory.getCanonicalFile());
        List<String> filePathParts = splitPath(file.getCanonicalFile());
        int commonDepth = 0;
        int maxCommonDepth = Math.min(outputDirectoryPathParts.size(), filePathParts.size());
        for (int i = 0; (i < maxCommonDepth)
                && outputDirectoryPathParts.get(i).equals(filePathParts.get(i)); i++) {
            commonDepth++;
        }
        // If there is a common root build a relative path between the common roots
        if (commonDepth > 0) {
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("..");
            for (int i = 0; i < outputDirectoryPathParts.size() - commonDepth - 1; i++) {
                stringBuilder.append(File.separator);
                stringBuilder.append("..");
            }
            for (int i = commonDepth; i < filePathParts.size(); i++) {
                stringBuilder.append(File.separator);
                stringBuilder.append(filePathParts.get(i));
            }
            resolvedPath = stringBuilder.toString();
        } else {
            if (IS_WINDOWS) {
                // Windows has no common root directory, cannot resolve a path
                // across drives so ...
                throw new IOException("Unable to resolve relative path across windows drives");
            }

            // no intersection between paths so calculate a path via the root directory
            final StringBuilder stringBuilder = new StringBuilder();

            File depthCheck = outputDirectory.getParentFile();
            while (depthCheck != null) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append(File.separator);
                }
                stringBuilder.append("..");
                depthCheck = depthCheck.getParentFile();
            }

            resolvedPath = stringBuilder.toString() + strFilePath;
        }
    }

    //System.out.println( "resolvedPath = " + resolvedPath );
    return resolvedPath;
}

From source file:com.parse.ParseFileUtils.java

/**
 * Determines whether the specified file is a Symbolic Link rather than an actual file.
 * <p>// w ww  .j ava 2  s  .  c  om
 * Will not return true if there is a Symbolic Link anywhere in the path,
 * only if the specific file is.
 * <p>
 * <b>Note:</b> the current implementation always returns {@code false} if the system
 * is detected as Windows using {@link FilenameUtils#isSystemWindows()}
 * <p>
 * For code that runs on Java 1.7 or later, use the following method instead:
 * <br>
 * {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
 * @param file the file to check
 * @return true if the file is a Symbolic Link
 * @throws IOException if an IO error occurs while checking the file
 * @since 2.0
 */
public static boolean isSymlink(final File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    //    if (FilenameUtils.isSystemWindows()) {
    //      return false;
    //    }
    File fileInCanonicalDir = null;
    if (file.getParent() == null) {
        fileInCanonicalDir = file;
    } else {
        final File canonicalDir = file.getParentFile().getCanonicalFile();
        fileInCanonicalDir = new File(canonicalDir, file.getName());
    }

    if (fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())) {
        return false;
    } else {
        return true;
    }
}

From source file:org.jsweet.input.typescriptdef.util.Util.java

private static String getReferenceMainModuleName(Context context, CompilationUnit compilationUnit,
        String reference) throws IOException {
    if (reference == null) {
        return null;
    }//w w w.j av  a  2  s  .  c  o m
    File referenceFile = new File(reference);
    if (!referenceFile.isAbsolute()) {
        referenceFile = new File(compilationUnit.getFile().getParentFile(), reference);
        referenceFile = referenceFile.getCanonicalFile();
    }
    CompilationUnit referencedCompilationUnit = null;
    for (CompilationUnit cu : context.compilationUnits) {
        if (cu.getFile().getCanonicalFile().equals(referenceFile)) {
            referencedCompilationUnit = cu;
        }
    }
    return referencedCompilationUnit == null ? null : referencedCompilationUnit.getMainModule().getName();
}

From source file:org.pentaho.reporting.designer.core.settings.SettingsUtil.java

public static File computeInstallationDirectory() throws IOException {
    final URL location = WorkspaceSettings.class.getProtectionDomain().getCodeSource().getLocation();
    LOG.debug("InstallationDirectory: Protection-Domain: " + location); // NON-NLS
    if (location == null) {
        return null;
    }/* w ww  .j  a  v  a  2 s  .co  m*/
    if ("file".equals(location.getProtocol()) == false) {
        LOG.debug("InstallationDirectory: Protection-Domain: Protocol failure."); // NON-NLS
        return null;
    }
    try {
        File jarPositon = new File(location.getFile());
        if (jarPositon.isFile() == false) {
            final Configuration configuration = ClassicEngineBoot.getInstance().getGlobalConfig();
            final String file = URLDecoder.decode(location.getFile(),
                    configuration.getConfigProperty("file.encoding", "ISO-8859-1")); // NON-NLS
            jarPositon = new File(file);
        }
        LOG.debug("InstallationDirectory: JAR file: " + jarPositon); // NON-NLS
        if (jarPositon.isFile()) {
            // secret knowledge here: We know all jars are in the lib-directory.
            final File libDirectory = jarPositon.getCanonicalFile().getParentFile();
            if (libDirectory == null) {
                LOG.debug("InstallationDirectory: No lib directory."); // NON-NLS
                return null;
            }
            LOG.debug("InstallationDirectory: Work directory: " + libDirectory.getParentFile()); // NON-NLS
            return libDirectory.getParentFile();
        }
    } catch (IOException ioe) {
        // ignore, but log.
        LOG.debug("InstallationDirectory: Failed to decode URL: ", ioe); // NON-NLS
    }

    // a directory, so we are running in an IDE.
    // hope for the best by using the current working directory.
    LOG.debug("InstallationDirectory: Work directory: Defaulting to current work directory."); // NON-NLS
    return new File(".");
}