Example usage for java.io File toURI

List of usage examples for java.io File toURI

Introduction

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

Prototype

public URI toURI() 

Source Link

Document

Constructs a file: URI that represents this abstract pathname.

Usage

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static String getRelativePath(File fileOrFolder, File baseFolder) {
    if (!baseFolder.isDirectory())
        baseFolder = baseFolder.getAbsoluteFile().getParentFile();

    return baseFolder.toURI().relativize(fileOrFolder.toURI()).getPath();
}

From source file:eu.openanalytics.rsb.config.ConfigurationFactory.java

private static URL getConfigurationUrl() {
    final File configurationFile = new File(RSB_CONFIGURATION_DIRECTORY, getConfigurationFileName());
    if (configurationFile.isFile() && configurationFile.canRead()) {
        try {/*from ww  w .  j  a  v a 2  s  . c  o  m*/
            return configurationFile.toURI().toURL();
        } catch (final MalformedURLException murle) {
            throw new IllegalStateException("Unexpected toURL failure for file: " + configurationFile, murle);
        }
    } else {
        return Thread.currentThread().getContextClassLoader().getResource(getConfigurationFileName());
    }
}

From source file:dk.statsbiblioteket.util.xml.XSLTTest.java

public static URL getURL(String resource) {
    if (resource == null) {
        return null;
    }//from  w  w w  . jav  a  2 s .  c o m
    try {
        return new URL(resource);
    } catch (MalformedURLException e) {
        // Nada error, just try the next
    }
    File file = new File(resource);
    if (file.exists() && file.canRead()) {
        try {
            return file.toURI().toURL();
        } catch (MalformedURLException e) {
            // Nada error, just try the next
        }
    }
    return Thread.currentThread().getContextClassLoader().getResource(resource);
}

From source file:edu.kit.dama.util.SystemUtils.java

/**
 * Wrapper for creating a symbolic link using java.nio
 *
 * @param pTargetFile The file the link will point to.
 * @param pLink The link location.// w w  w  . j av a2 s.co m
 *
 * @throws IOException if linking fails.
 */
public static void createSymbolicLink(File pTargetFile, File pLink) throws IOException {
    Path target = Paths.get(pTargetFile.toURI());
    Path link = Paths.get(pLink.toURI());
    Files.createSymbolicLink(link, target);
}

From source file:net.fabricmc.loom.task.ProcessModsTask.java

public static void addFile(File file, Object object) {
    try {/* ww  w  .  j  av a  2s .  c o m*/
        URLClassLoader classLoader = (URLClassLoader) object.getClass().getClassLoader();
        Class urlClassLoaderClass = URLClassLoader.class;
        Method method = urlClassLoaderClass.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, file.toURI().toURL());
    } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:com.t3.persistence.FileUtil.java

public static byte[] getBytes(File file) throws IOException {
    return getBytes(file.toURI().toURL());
}

From source file:io.druid.initialization.Initialization.java

public static List<URL> getURLsForClasspath(String cp) {
    try {/*from   w w w. j  a  v a2 s  .  c o m*/
        String[] paths = cp.split(File.pathSeparator);

        List<URL> urls = new ArrayList<>();
        for (int i = 0; i < paths.length; i++) {
            File f = new File(paths[i]);
            if ("*".equals(f.getName())) {
                File parentDir = f.getParentFile();
                if (parentDir.isDirectory()) {
                    File[] jars = parentDir.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            return name != null && (name.endsWith(".jar") || name.endsWith(".JAR"));
                        }
                    });
                    for (File jar : jars) {
                        urls.add(jar.toURI().toURL());
                    }
                }
            } else {
                urls.add(new File(paths[i]).toURI().toURL());
            }
        }
        return urls;
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.Resource.java

/** Construct a resource from a string.
 * @param resource A URL or filename.// w w w . j a v a2s.  co m
 * @return A Resource object.
 */
public static Resource newResource(String resource) throws MalformedURLException, IOException {
    URL url = null;
    try {
        // Try to format as a URL?
        url = new URL(resource);
    } catch (MalformedURLException e) {
        if (!resource.startsWith("ftp:") && !resource.startsWith("file:") && !resource.startsWith("jar:")) {
            try {
                // It's a file.
                if (resource.startsWith("./"))
                    resource = resource.substring(2);

                File file = new File(resource).getCanonicalFile();
                url = file.toURI().toURL();

                URLConnection connection = url.openConnection();
                FileResource fileResource = new FileResource(url, connection, file);
                return fileResource;
            } catch (Exception e2) {
                log.debug(LogSupport.EXCEPTION, e2);
                throw e;
            }
        } else {
            log.warn("Bad Resource: " + resource);
            throw e;
        }
    }

    String nurl = url.toString();
    if (nurl.length() > 0 && nurl.charAt(nurl.length() - 1) != resource.charAt(resource.length() - 1)) {
        if ((nurl.charAt(nurl.length() - 1) != '/'
                || nurl.charAt(nurl.length() - 2) != resource.charAt(resource.length() - 1))
                && (resource.charAt(resource.length() - 1) != '/'
                        || resource.charAt(resource.length() - 2) != nurl.charAt(nurl.length() - 1))) {
            return new BadResource(url, "Trailing special characters stripped by URL in " + resource);
        }
    }
    return newResource(url);
}

From source file:com.google.dart.tools.core.internal.util.ResourceUtil.java

/**
 * Return the {@link IResource} associated with the given Java {@link File}.
 * <p>/* w  w w. java 2 s  .c  om*/
 * If the {@link File} is an existing directory, then {@link IContainer} is returned.
 * <p>
 * If the {@link File} is an existing file or does not exist in the file system, then
 * {@link IFile} is returned.
 * <p>
 * If the {@link File} does not represent a valid part in the workspace, then {@code null} is
 * returned.
 */
public static IResource getResource(File file) {
    if (file == null) {
        return null;
    }
    if (file.isDirectory()) {
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IContainer[] containers = workspaceRoot.findContainersForLocationURI(file.toURI());
        if (containers.length == 0) {
            return null;
        }
        return containers[0];
    }
    // probably IFile
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IFile[] files = workspaceRoot.findFilesForLocationURI(file.toURI());
    if (files.length == 0) {
        return null;
    }
    return files[0];
}

From source file:net.nifheim.beelzebu.coins.common.utils.dependencies.DependencyManager.java

private static void loadJar(File file) throws RuntimeException {
    // get the classloader to load into
    ClassLoader classLoader = core.getMethods().getPlugin().getClass().getClassLoader();

    if (classLoader instanceof URLClassLoader) {
        try {//from w  ww  . ja v a 2s  .c o  m
            ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL());
        } catch (IllegalAccessException | InvocationTargetException | MalformedURLException e) {
            throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e);
        }
    } else {
        throw new RuntimeException("Unknown classloader type: " + classLoader.getClass());
    }
}