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.jcodeing.k_sonic.explorer.FileExplorerActivity.java

@Subscribe
public void onClickFile(FileExplorerEvents.OnClickFile event) {
    File f = event.mFile;
    try {//from  w  w  w  .  jav a 2s .com
        f = f.getAbsoluteFile();
        f = f.getCanonicalFile();
        if (TextUtils.isEmpty(f.toString()))
            f = new File("/");
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (f.isDirectory()) {
        String path = f.toString();
        mSettings.setLastDirectory(path);
        doOpenDirectory(path, true);
    } else if (f.exists()) {
        mSettings.setLastFile(f.getPath());
        finish();
    }
}

From source file:org.apache.accumulo.maven.plugin.StartMojo.java

@Override
public void execute() throws MojoExecutionException {
    if (shouldSkip()) {
        return;/*from   w  w  w .jav a  2s.  c  om*/
    }

    File subdir = new File(new File(outputDirectory, "accumulo-maven-plugin"), instanceName);

    try {
        subdir = subdir.getCanonicalFile();
        if (subdir.exists())
            FileUtils.forceDelete(subdir);
        if (!subdir.mkdirs() && !subdir.isDirectory())
            throw new IOException(subdir + " cannot be created as a directory");
        MiniAccumuloConfigImpl cfg = new MiniAccumuloConfigImpl(subdir, rootPassword);
        cfg.setInstanceName(instanceName);
        cfg.setZooKeeperPort(zooKeeperPort);
        configureMiniClasspath(cfg, miniClasspath);
        MiniAccumuloClusterImpl mac = new MiniAccumuloClusterImpl(cfg);
        getLog().info(
                "Starting MiniAccumuloCluster: " + mac.getInstanceName() + " in " + mac.getConfig().getDir());
        mac.start();
        runningClusters.add(mac);
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to start " + MiniAccumuloCluster.class.getSimpleName(), e);
    }

}

From source file:com.parallax.server.blocklyprop.servlets.HelpServlet.java

public boolean isSubDirectory(File base, File child) throws IOException {
    base = base.getCanonicalFile();
    child = child.getCanonicalFile();//from  w  ww  .  ja  v  a 2  s  .  c o  m

    File parentFile = child;
    while (parentFile != null) {
        if (base.equals(parentFile)) {
            return true;
        }
        parentFile = parentFile.getParentFile();
    }
    return false;
}

From source file:de.codesourcery.jasm16.compiler.io.FileResourceResolver.java

private File resolveCanonical(String identifier, File parentFile) {
    File canonical = new File(parentFile, identifier);
    try {/*ww w .  java  2  s . com*/
        canonical = canonical.getCanonicalFile();
    } catch (IOException e) {
        throw new RuntimeException("While resolving '" + canonical.getAbsolutePath() + "'", e);
    }
    return canonical;
}

From source file:org.apache.tomcat.util.compat.Jdk14Compat.java

/**
 *  Return the URI for the given file. Originally created for
 *  o.a.c.loader.WebappClassLoader//from   w ww .ja va 2 s  .  c o m
 *
 *  @param File to wrap into URI
 *  @return A URI as a URL
 */
public URL getURI(File file) throws MalformedURLException {

    File realFile = file;
    try {
        realFile = realFile.getCanonicalFile();
    } catch (IOException e) {
        // Ignore
    }

    return realFile.toURI().toURL();
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.vstest.runner.VsTestConfigFinder.java

private File findSameFileUpwards(File solutionDirectory, String setting) {
    String fullPath = solutionDirectory.getAbsolutePath() + "\\" + setting;
    File file = new File(fullPath);

    try {/* w  w  w.  j  av a 2s. c  om*/
        file = file.getCanonicalFile();
    } catch (IOException e) {
        String msg = "IOException on " + file.getAbsolutePath();
        LOG.error(msg);
        throw new SonarException(msg, e);
    }
    File folder = file.getParentFile();
    String name = file.getName();
    while (folder != null) {
        String fullName = folder.getAbsolutePath() + "\\" + name;
        file = new File(fullName);
        if (file.exists()) {
            return file;
        }
        folder = folder.getParentFile();
    }
    return null;
}

From source file:de.micromata.genome.util.runtime.CwdTest.java

@Test
public void testCwd() {
    String oldpwd = System.getProperty("user.dir");
    try {/* w  w w.j  a v  a 2s  . com*/
        File curFile = new File("pom.xml");
        String pom1 = FileUtils.readFileToString(curFile, Charset.defaultCharset());
        // Ausgabe: ./pom.xml: C:\Users\roger\d\micromata\genome\genome-commons\pom.xml
        System.out.println("./pom.xml: " + curFile.getAbsolutePath());
        File parentDir = new File("./..");
        // setze user dir auf parent file
        System.setProperty("user.dir", parentDir.getCanonicalFile().getAbsolutePath());
        File pafile = new File("pom.xml");
        // das gibt den namen parent pom aus!!!!
        // Ausgabe: new cwd: (..)./pom.xml: C:\Users\roger\d\micromata\genome\pom.xml
        System.out.println("new cwd: (..)./pom.xml: " + pafile.getAbsolutePath());
        // !!!!!!
        // das liest trotzdem das pom1 aus!!!
        String pom2 = FileUtils.readFileToString(pafile, Charset.defaultCharset());
        Assert.assertEquals(pom1, pom2);
        // das fixt das: 
        File pafile2 = pafile.getAbsoluteFile();
        String pom3 = FileUtils.readFileToString(pafile2, Charset.defaultCharset());
        // jetzt tatsaechlich das parent pom
        Assert.assertNotEquals(pom1, pom3);
    } catch (IOException ex) {
        ex.printStackTrace();
        ;
    } finally {
        System.setProperty("user.dir", oldpwd);
    }
}

From source file:org.eclipse.ecr.runtime.deploy.ConfigurationDeployer.java

public void undeploy(File file) throws Exception {
    undeploy(file.getCanonicalFile().toURI().toURL());
}

From source file:hu.bme.mit.sette.common.descriptors.java.JavaSourceFile.java

/**
 * Creates an instance of the {@link JavaSourceFile} object by using the
 * specified source and binary directories and the specified source file.
 *
 * @param pSourceDirectory//from  w  ww.  j a v  a 2  s  .com
 *            the source directory (e.g. /path/to/project/src)
 * @param pSourceFile
 *            the source file (e.g.
 *            /path/to/project/src/hu/bme/mit/sette/MyClass.java)
 * @param classLoader
 *            the class loader to be used to load the class
 * @return A {@link JavaSourceFile} object representing the Java file.
 * @throws SetteConfigurationException
 *             If an error occurred, e.g. not enough permissions to access
 *             the directory or the file, cannot retrieve canonical file
 *             names, file is in the specified directory or cannot load the
 *             Java class contained by the file.
 */
public static JavaSourceFile fromFile(final File pSourceDirectory, final File pSourceFile,
        final ClassLoader classLoader) throws SetteConfigurationException {
    Validate.notNull(pSourceDirectory, "The source directory must not be null");
    Validate.notNull(pSourceFile, "The source file must not be null");
    Validate.notNull(classLoader, "The class loader must not be null");

    try {
        // validate permissions
        GeneralValidator v = new GeneralValidator(JavaSourceFile.class);

        FileValidator v1 = new FileValidator(pSourceDirectory);
        v1.type(FileType.DIRECTORY).readable(true).executable(true);
        v.addChildIfInvalid(v1);

        FileValidator v2 = new FileValidator(pSourceFile);
        v2.type(FileType.REGULAR_FILE).readable(true);
        v2.extension(JavaFileUtils.JAVA_SOURCE_EXTENSION);
        v.addChildIfInvalid(v2);

        v.validate();

        // get canonical file objects
        File sourceDirectory = pSourceDirectory.getCanonicalFile();
        File sourceFile = pSourceFile.getCanonicalFile();

        // get paths
        // like "/path/to/project/src"
        String sourceDirectoryPath = FilenameUtils.normalizeNoEndSeparator(sourceDirectory.getAbsolutePath());
        // like "/path/to/project/src/pkg/path/here/MyClass.java"
        String sourceFilePath = FilenameUtils.normalizeNoEndSeparator(sourceFile.getAbsolutePath());

        // check whether the file is under the specified directory
        if (FilenameUtils.directoryContains(sourceDirectoryPath, sourceFilePath)) {
            // get relative path and class name
            // like "pkg/path/here/MyClass.java"
            String classPath = sourceFilePath.substring(sourceDirectoryPath.length() + 1);

            // like "pkg.path.here.MyClass"
            String className = JavaFileUtils.filenameToClassName(classPath);

            // load class
            Class<?> javaClass = classLoader.loadClass(className);

            // create object and return with it
            return new JavaSourceFile(sourceFile, javaClass);
        } else {
            String message = String.format("The source file is not in the " + "source directory\n"
                    + "(sourceDirectory: [%s])\n(sourceFile: [%s])", sourceDirectory, sourceFile);
            throw new SetteConfigurationException(message);
        }
    } catch (ValidatorException e) {
        throw new SetteConfigurationException("A validation exception occurred", e);
    } catch (IOException e) {
        throw new SetteConfigurationException("An IO exception occurred", e);
    } catch (ClassNotFoundException e) {
        throw new SetteConfigurationException("The Java class could not have been loaded", e);
    }
}

From source file:org.nuxeo.runtime.deploy.ConfigurationDeployer.java

public void undeploy(File file) throws IOException {
    undeploy(file.getCanonicalFile().toURI().toURL());
}