Java ClassLoader createDir(String pathName)

Here you can find the source of createDir(String pathName)

Description

create Dir

License

Apache License

Declaration

public static File createDir(String pathName) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

import java.net.URL;

public class Main {
    public static File createDir(String pathName) throws Exception {
        File dir = new File(getClasspathRootDir() + pathName);
        if (!dir.exists())
            dir.mkdir();/*from w  w w .  j  a  va2  s.c  o m*/
        return dir;
    }

    public static String getClasspathRootDir() throws Exception {
        File tmpRoot = getClassPathFile("test.properties").getParentFile();
        return tmpRoot.getCanonicalPath().replaceAll("%20", " ");
    }

    public static File getClassPathFile(String pathName) throws Exception {
        // In windows, pathnames with spaces are returned as %20
        if (pathName.indexOf("%20") != -1)
            pathName = pathName.replaceAll("%20", " ");
        File tmp = new File(getResourceURL(pathName).getFile());
        return tmp;
    }

    public static URL getResourceURL(String resource) throws IOException {
        URL url = null;
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader != null)
            url = loader.getResource(resource);
        if (url == null)
            url = ClassLoader.getSystemResource(resource);
        if (url == null)
            throw new IOException("Resource " + resource + " was not found");
        return url;
    }
}

Related

  1. add(File file)
  2. addDirectory(File directory)
  3. AddFile(File f)
  4. addFile(File file)
  5. addFile(String s)
  6. createDirectoryLoader(String directory)
  7. CreateLogbackXML(OutputStream out)
  8. findFile(String filename)
  9. hasImageFile(String name)