Example usage for java.net URL getFile

List of usage examples for java.net URL getFile

Introduction

In this page you can find the example usage for java.net URL getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the file name of this URL .

Usage

From source file:net.grinder.util.GrinderClassPathUtils.java

/**
 * Construct the foremost classPath from current classLoader.
 * // w  w w.  j  a  v a 2s.c om
 * @param logger
 *            logger
 * @return classpath optimized for grinder.
 */
public static String buildForemostClasspathBasedOnCurrentClassLoader(Logger logger) {
    URL[] urLs = ((URLClassLoader) GrinderClassPathUtils.class.getClassLoader()).getURLs();
    StringBuilder builder = new StringBuilder();
    for (URL each : urLs) {
        builder.append(each.getFile()).append(File.pathSeparator);
    }
    return GrinderClassPathUtils.filterForeMostClassPath(builder.toString(), logger);
}

From source file:net.grinder.util.GrinderClassPathUtils.java

/**
 * Construct classPath from current classLoader.
 * //from  w ww.java 2  s.  co  m
 * @param logger
 *            logger
 * @return classpath optimized for grinder.
 */
public static String buildClasspathBasedOnCurrentClassLoader(Logger logger) {
    URL[] urLs = ((URLClassLoader) GrinderClassPathUtils.class.getClassLoader()).getURLs();
    StringBuilder builder = new StringBuilder();
    for (URL each : urLs) {
        builder.append(each.getFile()).append(File.pathSeparator);
    }
    return GrinderClassPathUtils.filterClassPath(builder.toString(), logger);
}

From source file:main.java.com.aosa.util.AOSAReadProperties.java

/**
 * Description<code>Properties</code>      <br>
 * By mutou at 2012-1-12 ?05:58:16   <br>
 * boolean      <br>//  w  w w .  ja  v  a 2s. c o  m
 * @param profileName
 *       Properties ??ClassPath
 * @param key
 *       ?? 
 * @param value
 *       ? value
 * @return
 * @throws
 */
public static boolean WriteValue(String profileName, String key, String value) {
    boolean isWrite = false;
    Properties properties = new Properties();
    InputStream input = AOSAReadProperties.class.getClassLoader().getResourceAsStream(profileName);
    try {
        properties.load(input);
        properties.setProperty(key, value);
        URL prourl = AOSAReadProperties.class.getClassLoader().getResource(profileName);
        String filepath = prourl.getFile();
        FileOutputStream fileOutputStream = new FileOutputStream(filepath);
        properties.store(fileOutputStream, "Custum shutDownTime config : conf.properties");
        fileOutputStream.flush();
        fileOutputStream.close();
        isWrite = true;
    } catch (FileNotFoundException e) {
        logger.debug("Properties");
        throw new AOSARuntimeException("Properties", e);
    } catch (IOException e) {
        logger.debug("PropertiesIO");
        throw new AOSARuntimeException("PropertiesIO", e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                logger.debug("IO?");
                throw new AOSARuntimeException("IO?", e);
            }
        }
    }
    return isWrite;
}

From source file:com.ibm.wala.cast.ipa.callgraph.CAstCallGraphUtil.java

public static SourceFileModule makeSourceModule(URL script, String scriptName) {
    String hackedName = script.getFile().replaceAll("%5c", "/").replaceAll("%20", " ");

    File scriptFile = new File(hackedName);

    assert hackedName.endsWith(scriptName) : scriptName + " does not match file " + script.getFile();

    return new SourceFileModule(scriptFile, scriptName, null) {
        @Override/*from  www  . j a va  2s  .  c om*/
        public InputStream getInputStream() {
            BOMInputStream bs = new BOMInputStream(super.getInputStream(), false, ByteOrderMark.UTF_8,
                    ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE,
                    ByteOrderMark.UTF_32BE);
            try {
                if (bs.hasBOM()) {
                    System.err.println("removing BOM " + bs.getBOM());
                }
                return bs;
            } catch (IOException e) {
                return super.getInputStream();
            }
        }
    };
}

From source file:ReflectUtils.java

/**
 * Scans all classes accessible from the context class loader which belong to the given package and subpackages.
 *
 * @param packageName The base package//from   ww  w.ja  v  a2s  . co m
 * @return The classes
 * @throws ClassNotFoundException
 * @throws IOException
 */
private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException {
    //        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
}

From source file:com.shishu.utility.url.TableUtil.java

/**
 * Reverses a url's domain. This form is better for storing in hbase. Because
 * scans within the same domain are faster.
 * <p>/*w w w  . j  a  v  a 2  s  .  c o  m*/
 * E.g. "http://bar.foo.com:8983/to/index.html?a=b" becomes
 * "com.foo.bar:http:8983/to/index.html?a=b".
 *
 * @param url
 *          url to be reversed
 * @return Reversed url
 */
public static String reverseUrl(URL url) {
    String host = url.getHost();
    String file = url.getFile();
    String protocol = url.getProtocol();
    int port = url.getPort();

    StringBuilder buf = new StringBuilder();

    /* reverse host */
    reverseAppendSplits(host, buf);

    /* add protocol */
    buf.append(':');
    buf.append(protocol);

    /* add port if necessary */
    if (port != -1) {
        buf.append(':');
        buf.append(port);
    }

    /* add path */
    if (file.length() > 0 && '/' != file.charAt(0)) {
        buf.append('/');
    }
    buf.append(file);

    return buf.toString();
}

From source file:FileUtil.java

/**
 * Gets file date and time.//www  .j a v  a2 s .c o  m
 *
 * @param url The URL of the file for which date and time will be returned.
 * @return Returns long value which is the date and time of the file. If any error
 *         occures returns -1 (=no file date and time available).
 */
public static long getFileDateTime(URL url) {
    if (url == null) {
        return -1;
    }

    String fileName = url.getFile();
    if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\') {
        fileName = fileName.substring(1, fileName.length());
    }

    try {
        File file = new File(fileName);
        // File name must be a file or a directory.
        if (!file.isDirectory() && !file.isFile()) {
            return -1;
        }

        return file.lastModified();
    } catch (java.lang.Exception e) { // Trap all Exception based exceptions and return -1.
        return -1;
    }
}

From source file:com.amalto.core.server.MockServer.java

static String getDatasourcesFilePath() {
    URL url = MockServer.class.getClassLoader().getResource(DATASOURCES_FILE);
    junit.framework.Assert.assertNotNull(url);
    return new File(url.getFile()).getAbsolutePath();
}

From source file:PropertiesUtil.java

public static void store(Properties properties, Class relativeClass, String relativeFileName)
        throws IOException {
    if (relativeClass == null)
        throw new IllegalArgumentException("Relative Class is not set.");
    if (relativeFileName == null) {
        throw new IllegalArgumentException("Relative File Name is not set.");
    } else {/*from   w  ww . j ava  2 s. c  o  m*/
        String className = relativeClass.getName().substring(relativeClass.getName().lastIndexOf(".") + 1);
        URL url = relativeClass.getResource(String.valueOf(String.valueOf(className)).concat(".class"));
        String fileName = url.getFile();
        fileName = String.valueOf(fileName.substring(1, fileName.length() - className.length() - 6))
                + String.valueOf(relativeFileName);
        store(properties, fileName);
        return;
    }
}

From source file:es.emergya.consultas.FlotaConsultas.java

public static List<String> getAllIcons(String dir) {
    List<String> res = new ArrayList<String>();
    try {/*from  ww w.  j  a  v  a2s  . c o m*/
        URL u = FlotaConsultas.class.getResource(dir);
        if (u != null) {
            File f = new File(u.getFile());
            if (f.isDirectory()) {
                FilenameFilter filter = new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.endsWith("_preview.png");
                    }
                };
                for (String path : f.list(filter))
                    res.add(path.substring(0, path.indexOf("_flota_preview")));
            }
        }
    } catch (Throwable t1) {
        log.error(t1, t1);
    }

    return res;
}