Example usage for java.io File isAbsolute

List of usage examples for java.io File isAbsolute

Introduction

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

Prototype

public boolean isAbsolute() 

Source Link

Document

Tests whether this abstract pathname is absolute.

Usage

From source file:org.apache.druid.initialization.Initialization.java

/**
 * Find all the extension files that should be loaded by druid.
 * <p/>//from   w  ww  .  j ava  2s .  co  m
 * If user explicitly specifies druid.extensions.loadList, then it will look for those extensions under root
 * extensions directory. If one of them is not found, druid will fail loudly.
 * <p/>
 * If user doesn't specify druid.extension.toLoad (or its value is empty), druid will load all the extensions
 * under the root extensions directory.
 *
 * @param config ExtensionsConfig configured by druid.extensions.xxx
 *
 * @return an array of druid extension files that will be loaded by druid process
 */
public static File[] getExtensionFilesToLoad(ExtensionsConfig config) {
    final File rootExtensionsDir = new File(config.getDirectory());
    if (rootExtensionsDir.exists() && !rootExtensionsDir.isDirectory()) {
        throw new ISE("Root extensions directory [%s] is not a directory!?", rootExtensionsDir);
    }
    File[] extensionsToLoad;
    final LinkedHashSet<String> toLoad = config.getLoadList();
    if (toLoad == null) {
        extensionsToLoad = rootExtensionsDir.listFiles();
    } else {
        int i = 0;
        extensionsToLoad = new File[toLoad.size()];
        for (final String extensionName : toLoad) {
            File extensionDir = new File(extensionName);
            if (!extensionDir.isAbsolute()) {
                extensionDir = new File(rootExtensionsDir, extensionName);
            }

            if (!extensionDir.isDirectory()) {
                throw new ISE("Extension [%s] specified in \"druid.extensions.loadList\" didn't exist!?",
                        extensionDir.getAbsolutePath());
            }
            extensionsToLoad[i++] = extensionDir;
        }
    }
    return extensionsToLoad == null ? new File[] {} : extensionsToLoad;
}

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

/**
 * Helper class that tries to load an existing file from various location, 
 * including direct file location, relative to classpath, relative to project source.
 * The order of search is: absolute, relative to source, relative to classpath.
 * @param name/*from   ww w  .  ja v  a  2s .c o m*/
 * @return
 * @throws FileNotFoundException 
 */
public static InputStream getFileStream(String name) throws FileNotFoundException {
    File file = new File(name);
    if (file.isAbsolute()) {
        return new FileInputStream(file);
    } else {
        // check relative to source
        if (file.exists()) {
            return new FileInputStream(file);
        } else {
            return FileUtil.class.getClassLoader().getResourceAsStream(name);
        }
    }
}

From source file:PathUtils.java

/**
 * Calculates the relative path between a specified root directory and a target path.
 *
 * @param root   The absolute path of the root directory.
 * @param target The path to the target file or directory.
 * @return The relative path between the specified root directory and the target path.
 * @throws IllegalArgumentException <ul><li>The root file cannot be null.</li><li>The target cannot be
 *                                  null.</li><li>The root file must be a directory.</li><li>The root file must be
 *                                  absolute.</li></ul>
 *//*from w w  w. j a v  a2 s  . c o  m*/
public static String relativize(final File root, final File target) throws IllegalArgumentException {
    if (root == null)
        throw new IllegalArgumentException("The root file cannot be null.");
    if (target == null)
        throw new IllegalArgumentException("The target cannot be null.");
    if (!root.isDirectory())
        throw new IllegalArgumentException("The root file must be a directory.");
    if (!root.isAbsolute())
        throw new IllegalArgumentException("The root file must be absolute.");
    if (!target.isAbsolute())
        return target.toString();

    if (root.equals(target))
        return ".";

    // Deconstruct hierarchies
    final Deque<File> rootHierarchy = new ArrayDeque<File>();
    for (File f = root; f != null; f = f.getParentFile())
        rootHierarchy.push(f);
    final Deque<File> targetHierarchy = new ArrayDeque<File>();
    for (File f = target; f != null; f = f.getParentFile())
        targetHierarchy.push(f);

    // Trace common root
    while (rootHierarchy.size() > 0 && targetHierarchy.size() > 0
            && rootHierarchy.peek().equals(targetHierarchy.peek())) {
        rootHierarchy.pop();
        targetHierarchy.pop();
    }
    // Create relative path
    final StringBuilder sb = new StringBuilder(rootHierarchy.size() * 3 + targetHierarchy.size() * 32);
    while (rootHierarchy.size() > 0) {
        sb.append("..");
        rootHierarchy.pop();
        if (rootHierarchy.size() > 0 || targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    while (targetHierarchy.size() > 0) {
        sb.append(targetHierarchy.pop().getName());
        if (targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    return sb.toString();
}

From source file:org.dataconservancy.dcs.util.FilePathUtil.java

/**
 * This method has been deprecated you should now use Java's Path.resolve method.
 * Takes a relative file path and makes it absolute by prepending the base path.
 * If the provided file path is already absolute it's returned without modification.
 * @param baseFile The base file that will be used to make the file path absolute.
 * @param toAbsolutize The file to make absolute by adding it to the file base
 * @return A java File object pointing to the absolute path to the file, or null if the file object, or base file is null.
 *//* w  w  w. j  a v  a  2 s.  c  o  m*/
@Deprecated
public static File absolutize(File baseFile, File toAbsolutize) {
    if (toAbsolutize == null || baseFile == null) {
        return null;
    }

    // If the file is already absolute, there's nothing for us to do here.
    if (toAbsolutize.isAbsolute()) {
        return toAbsolutize;
    }

    Path basePath = baseFile.toPath().resolve(toAbsolutize.toPath());

    return basePath.toFile();
}

From source file:org.apache.falcon.recipe.RecipeTool.java

private static void createWindowsLocalPaths(final Properties recipeProperties) {
    if (Shell.WINDOWS) {
        String path = recipeProperties.getProperty(RecipeToolOptions.WORKFLOW_PATH.getName());
        File file = null;
        if (StringUtils.isNotEmpty(path)) {

            recipeProperties.setProperty(RecipeToolOptions.WORKFLOW_PATH.getName() + WINDOWS_SUFFIX, path);
            file = new File(path);
            if (file.isAbsolute()) {
                // UNC paths are not supported and are not handled
                path = file.getAbsolutePath().substring(2); // Get rid of the Drive:
            }/*from  w w  w .  java2  s .  c  om*/
            path = path.replaceAll("\\\\", "/");
            // replace the properties file
            recipeProperties.setProperty(RecipeToolOptions.WORKFLOW_PATH.getName(), path);
        }
        path = recipeProperties.getProperty(RecipeToolOptions.WORKFLOW_LIB_PATH.getName());
        if (StringUtils.isNotEmpty(path)) {
            recipeProperties.setProperty(RecipeToolOptions.WORKFLOW_LIB_PATH.getName() + WINDOWS_SUFFIX, path);
            file = new File(path);
            if (file.isAbsolute()) {
                // UNC paths are not supported and are not handled
                path = file.getAbsolutePath().substring(2); // Get rid of the Drive:
            }
            path = path.replaceAll("\\\\", "/");
            // replace the properties file
            recipeProperties.setProperty(RecipeToolOptions.WORKFLOW_LIB_PATH.getName(), path);
        }
    }
}

From source file:org.cloudifysource.shell.installer.ManagementWebServiceInstaller.java

/**
 * Gets the service's war file. If the war file's path is not an absolute path, it is considered relative
 * to the home directory./*from   ww  w  . j  av  a2s.  c o m*/
 *
 * @param warFile
 *            The service's war file object (possibly with a relative path)
 * @return The service's war file object
 */
public static File getGSFile(final File warFile) {
    File absWarFile = warFile;
    if (!absWarFile.isAbsolute()) {
        absWarFile = new File(Environment.getHomeDirectory(), warFile.getPath());
    }
    return absWarFile;
}

From source file:com.legstar.codegen.CodeGenUtil.java

/**
 * Retrieve a file. Given a directory and a filename, this creates a File
 * according to the following rules://from  w  ww.  j a  v a2 s. c om
 * <ul>
 * <li>If the filename is absolute, the directory name is ignored</li>
 * <li>Otherwise, filename is appended to directory</li>
 * </ul>
 * 
 * @param fdir parent directory
 * @param filename absolute or relative file name
 * @return a File
 */
public static File getFile(final File fdir, final String filename) {
    File file = new File(filename);
    if (file.isAbsolute()) {
        return file;
    }
    return new File(fdir, filename);
}

From source file:com.legstar.codegen.CodeGenUtil.java

/**
 * Retrieve a file. Given a directory name and a filename, this creates a
 * File according to the following rules:
 * <ul>/*from w  ww. j a  va 2 s .c  o m*/
 * <li>If the filename is absolute, the directory name is ignored</li>
 * <li>If the directory is not null, it is assumed to exist</li>
 * <li>If the directory is not null and the filename is not absolute, then
 * filename is appended to directory</li>
 * </ul>
 * 
 * @param dir parent directory
 * @param filename absolute or relative file name
 * @return a File
 */
public static File getFile(final String dir, final String filename) {
    File file = new File(filename);
    if (file.isAbsolute()) {
        return file;
    }
    if (dir == null || dir.length() == 0) {
        return new File(filename);
    }
    return new File(dir, filename);
}

From source file:org.globus.workspace.client_core.utils.FileUtils.java

public static String readFileAsString(String path) throws IOException {

    final File file = new File(path);

    if (!file.exists()) {
        String err = "File does not exist, path: '" + path + "'";
        if (!file.isAbsolute()) {
            err += " (absolute path: '" + file.getAbsolutePath() + "')";
        }//from   w  w  w  .  ja va 2  s.c om
        throw new IOException(err);
    }

    if (!file.canRead()) {
        String err = "Cannot read file: \"" + path + "\"";
        if (!file.isAbsolute()) {
            err += " (absolute path: \"" + file.getAbsolutePath() + "\")";
        }
        throw new IOException(err);
    }

    final StringBuffer sb = new StringBuffer(1024);

    BufferedReader in = null;
    FileReader fr = null;
    String s;
    try {
        fr = new FileReader(file);
        in = new BufferedReader(fr);
        s = in.readLine();
        while (s != null) {
            sb.append(s);
            sb.append("\n");
            s = in.readLine();
        }
    } finally {
        if (fr != null) {
            fr.close();
        }
        if (in != null) {
            in.close();
        }
    }
    return sb.toString();
}

From source file:org.broad.igv.util.FileUtils.java

/**
 * Returns an absolute path from the parent directory
 * of {@code referencePath} to the sub-element {@code inputPath}.
 * Safe to use with URLs./*w  w  w.j a v  a2 s . com*/
 * If {@code inputPath} is an absolute path, it is returned unaltered.
 * <br/>
 * e.g.<br/>
 * String absPath = FileUtils.getAbsolutePath("test/mysession.xml", "/Users/bob/data/otherdata.xml");
 * System.out.println(absPath);<br/>
 * >>>> /Users/bob/data/test/mysession.xml
 *
 * @param inputPath     Relative path element
 * @param referencePath Absolute path root
 * @return
 */
public static String getAbsolutePath(String inputPath, String referencePath) {
    String absolutePath;

    if (isRemote(referencePath)) {
        if (isRemote(inputPath)) {
            return inputPath;
        }
        int idx = referencePath.lastIndexOf("/");
        String basePath = referencePath.substring(0, idx);
        absolutePath = basePath + "/" + inputPath;
    } else {
        File inFile = new File(inputPath);
        if (inFile.isAbsolute()) {
            return inFile.getAbsolutePath();
        }
        File parent = new File(referencePath).getParentFile();
        File file = new File(parent, inputPath);
        absolutePath = file.getAbsolutePath();
    }
    return absolutePath;
}