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.alfresco.bm.file.FileDataServiceTest.java

@BeforeClass
public static void setUp() throws IOException {
    // Create a test file
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File testFiles = new File(tempDir, "FileDataServiceTest");
    testFiles.mkdirs();//  ww w  . j a v  a2s . c o  m
    File testFile = new File(testFiles, "test.txt");
    if (!testFile.exists()) {
        String text = "SOME TEXT";
        Files.write(text, testFile, Charsets.UTF_8);
    }

    File localDir = new File(System.getProperty("java.io.tmpdir") + "/" + "fileset-123");
    if (localDir.exists()) {
        localDir.delete();
    }

    Properties props = new Properties();
    props.put("test.mongoCollection", COLLECTION_BM_FILE_DATA_SERVICE);
    props.put("test.localDir", localDir.getCanonicalFile());
    props.put("test.ftpHost", "ftp.mirrorservice.org");
    props.put("test.ftpPort", "21");
    props.put("test.ftpUsername", "anonymous");
    props.put("test.ftpPassword", "");
    props.put("test.ftpPath", "/sites/www.linuxfromscratch.org/images");
    props.put("test.testFileDir", testFiles.getCanonicalPath());

    ctx = new ClassPathXmlApplicationContext(new String[] { "test-MongoFileDataTest-context.xml" }, false);
    ctx.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("TestProps", props));
    ctx.refresh();
    ctx.start();

    // Get the new beans
    fileDataService = ctx.getBean(FileDataService.class);
    ftpTestFileService = ctx.getBean(FtpTestFileService.class);
    localTestFileService = ctx.getBean(LocalTestFileService.class);

    // Do a directory listing and use that as the dataset
    File randomDir = new File(System.getProperty("user.dir"));
    File[] localFiles = randomDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return !pathname.isDirectory();
        }
    });
    fileDatas = new FileData[localFiles.length];
    for (int i = 0; i < localFiles.length; i++) {
        String remoteName = localFiles[i].getName();
        if (remoteName.length() == 0) {
            continue;
        }
        fileDatas[i] = FileDataServiceTest.createFileData(remoteName);
    }
}

From source file:org.mycontroller.standalone.StartApp.java

private static void cleanUpServices() {
    // clean the services
    // - MQTT client location
    // - MQTT broker location

    try {/*w  ww  .  j  av  a  2s  .c  o  m*/
        File mqttClientDir = new File(AppProperties.getInstance().getMqttClientPersistentStoresLocation());
        if (mqttClientDir.exists()) {
            FileUtils.cleanDirectory(mqttClientDir);
            _logger.debug("MQTT Client persistent store cleared. [{}]", mqttClientDir.getCanonicalFile());
        }
        File mqttBrokerDir = new File(AppProperties.getInstance().getMqttBrokerPersistentStore())
                .getParentFile();
        if (mqttBrokerDir.exists()) {
            FileUtils.cleanDirectory(mqttBrokerDir);
            _logger.debug("MQTT broker persistent store cleared. [{}]", mqttBrokerDir.getCanonicalFile());
        }
    } catch (IOException ex) {
        _logger.error("Exception,", ex);
    }
}

From source file:com.stacksync.desktop.util.FileUtil.java

public static File getCanonicalFile(File file) {
    try {//from w w w . ja va 2s .c  o  m
        return file.getCanonicalFile();
    } catch (IOException ex) {
        return file;
    }
}

From source file:com.cedarsoft.io.LinkUtils.java

/**
 * Creates a link./*w  w w  . ja  v  a 2s  .  c  om*/
 * Returns true if the link has been created, false if the link (with the same link source) still exists.
 *
 * @param linkTarget the link source
 * @param linkFile   the link file
 * @param symbolic   whether to create a symbolic link
 * @return whether the link has been created (returns false if the link still existed)
 *
 * @throws IOException if something went wrong
 */
public static boolean createLink(@Nonnull File linkTarget, @Nonnull File linkFile, boolean symbolic)
        throws IOException {
    if (linkFile.exists()) {
        //Maybe the hard link still exists - we just don't know, so throw an exception
        if (!symbolic) {
            throw new IOException("link still exists " + linkFile.getAbsolutePath());
        }

        if (linkFile.getCanonicalFile().equals(linkTarget.getCanonicalFile())) {
            //still exists - that is ok, since it points to the same directory
            return false;
        } else {
            //Other target
            throw new IOException("A link still exists at <" + linkFile.getAbsolutePath()
                    + "> but with different target: <" + linkTarget.getCanonicalPath() + "> exected <"
                    + linkFile.getCanonicalPath() + ">");
        }
    }

    List<String> args = new ArrayList<String>();
    args.add("ln");
    if (symbolic) {
        args.add("-s");
    }
    args.add(linkTarget.getPath());
    args.add(linkFile.getAbsolutePath());

    ProcessBuilder builder = new ProcessBuilder(args);
    Process process = builder.start();
    try {
        int result = process.waitFor();
        if (result != 0) {
            throw new IOException("Creation of link failed: " + IOUtils.toString(process.getErrorStream()));
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    return true;
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

private static MavenProject findMavenProject(final File modulePath, Collection<MavenProject> modules)
        throws IOException {

    File canonical = modulePath.getCanonicalFile();
    for (MavenProject module : modules) {
        if (module.getBasedir().getCanonicalFile().equals(canonical)
                || module.getFile().getCanonicalFile().equals(canonical)) {
            return module;
        }/*  w w w  .j a  v a2  s. com*/
    }
    return null;
}

From source file:utils.GlobalParameters.java

public static void deleteFile(File fileToDelete) {
    fileToDelete.setWritable(true);// w w  w.  ja  v  a 2s . co  m
    try {
        fileToDelete = fileToDelete.getCanonicalFile();
    } catch (IOException e) {
        log.error("IP-Exception on CanonicalFile: " + fileToDelete.getAbsolutePath());
    }

    if (!fileToDelete.canWrite()) {
        log.trace("Set writable flag for file to delete");
        fileToDelete.setWritable(true);
    }

    boolean deleteFile = fileToDelete.delete();
    if (deleteFile) {
        log.info("Delete file: " + fileToDelete.getAbsolutePath() + " sucessfully.");
    } else {
        log.error("Cannot delete file: " + fileToDelete.getAbsolutePath());
        log.info("Try delete on exit.");
        fileToDelete.deleteOnExit();
    }
}

From source file:org.esa.s2tbx.dataio.s2.l1b.Sentinel2L1BProductReader.java

static File getProductDir(File productFile) throws IOException {
    final File resolvedFile = productFile.getCanonicalFile();
    if (!resolvedFile.exists()) {
        throw new FileNotFoundException("File not found: " + productFile);
    }/*from w ww  .jav  a2  s  .com*/

    if (productFile.getParentFile() == null) {
        return new File(".").getCanonicalFile();
    }

    return productFile.getParentFile();
}

From source file:org.ut.biolab.medsavant.shared.util.DirectorySettings.java

/**
 * Create a directory whose name includes a date-stamp.
 *
 * @param parent the parent directory, typically <code>new File(".")</code>
 *//*from  ww w. j  av  a 2s.c  o  m*/
public static File generateDateStampDirectory(File parent) throws IOException {
    File dir;
    do {
        Calendar today = new GregorianCalendar();
        String dateStamp = today.get(Calendar.YEAR) + "_" + (today.get(Calendar.MONTH) + 1) + "_"
                + today.get(Calendar.DAY_OF_MONTH) + "_" + today.get(Calendar.HOUR_OF_DAY) + "_"
                + today.get(Calendar.MINUTE) + "_" + today.get(Calendar.SECOND) + "_"
                + today.get(Calendar.MILLISECOND);
        dir = new File(parent.getCanonicalFile(), dateStamp);
        try {
            Thread.sleep(50);
        } catch (InterruptedException iex) {
        }
    } while (dir.exists());

    dir.mkdirs();
    return dir;
}

From source file:com.aliyun.odps.local.common.utils.LocalRunUtils.java

/**
 *  relativePath?parent???//  w  w  w  .ja  va  2  s . c o m
 * CanoncialFile????????
 * child??getParentFile?parent????
 * child???null
 * 
 * 
 * @param parent
 *          
 * @param realativePath
 *          ?
 * @throws IOException
 *           ??????
 */
private static void checkParent(File parent, String relativePath) {
    try {
        File child = new File(parent, relativePath);
        if (!child.exists()) {
            throw new IOException("ODPS-0140171: must set a correct realtive path:" + relativePath);
        }
        while (child != null && !child.getCanonicalFile().equals(parent.getCanonicalFile())) {
            if (FileUtils.isSymlink(child)) {
                throw new IOException("ODPS-0140171: not allow symlink in archive files:" + child.getName());
            }
            child = child.getParentFile();
        }
        if (child == null) {
            throw new IOException("ODPS-0140171: not correct parameter of relative path in getCacheArchive... :"
                    + relativePath);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        IOException tmp_e = new IOException("ODPS-0140171: permission denied to read archive resource '"
                + parent.getName() + "' with relative path :" + relativePath);
        throw new RuntimeException(tmp_e);
    }
}

From source file:com.hipu.bdb.util.FileUtils.java

public static File tryToCanonicalize(File file) {
    try {/*from   w  w  w  . j a  v a2 s .c om*/
        return file.getCanonicalFile();
    } catch (IOException e) {
        return file;
    }
}