当前位置: 首页>>代码示例>>Java>>正文


Java File.toURL方法代码示例

本文整理汇总了Java中java.io.File.toURL方法的典型用法代码示例。如果您正苦于以下问题:Java File.toURL方法的具体用法?Java File.toURL怎么用?Java File.toURL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.io.File的用法示例。


在下文中一共展示了File.toURL方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testHandleSpaceInPathAsProducedByEclipse

import java.io.File; //导入方法依赖的package包/类
public void testHandleSpaceInPathAsProducedByEclipse() throws Exception {
    File d = new File(getWorkDir(), "space in path");
    d.mkdirs();
    File f = new File(d, "x.jar");
    JarOutputStream os = new JarOutputStream(new FileOutputStream(f));
    os.putNextEntry(new JarEntry("test.txt"));
    os.write(10);
    os.close();
    
    URL u = new URL("jar:" + f.toURL() + "!/test.txt");
    DataInputStream is = new DataInputStream(u.openStream());
    byte[] arr = new byte[100];
    is.readFully(arr, 0, 1);
    assertEquals("One byte", 10, arr[0]);
    is.close();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:17,代码来源:ProxyURLStreamHandlerFactoryTest.java

示例2: getURLName

import java.io.File; //导入方法依赖的package包/类
public static String getURLName (FileObject fileObject) throws MalformedURLException, FileStateInvalidException {
        URL fileURL = null;
        File file = FileUtil.toFile (fileObject);

        if ( file != null ) {
//            if ( Util.THIS.isLoggable() ) /* then */ {
//                try {
//                    Util.THIS.debug ("[TransformUtil.getURLName]");
//                    Util.THIS.debug ("    file = " + file);
//                    Util.THIS.debug ("    file.getCanonicalPath = " + file.getCanonicalPath());
//                    Util.THIS.debug ("    file.getAbsolutePath  = " + file.getAbsolutePath());
//                    Util.THIS.debug ("    file.toString  = " + file.toString());
//                    Util.THIS.debug ("    file.toURL  = " + file.toURL());
//                } catch (Exception exc) {
//                    Util.THIS.debug ("DEBUG Exception", exc);
//                }
//            }

            fileURL = file.toURL();
        } else {
            fileURL = fileObject.getURL();
        }

        return fileURL.toExternalForm();
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:TransformUtil.java

示例3: testGetClasspathComponentsFromClassLoader

import java.io.File; //导入方法依赖的package包/类
public void testGetClasspathComponentsFromClassLoader() throws MalformedURLException
{
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try
    {
        File file = new File("/some/package/dir");
        URL[] urls = new URL[] { file.toURL() };            
        // assert my package can't be found yet
        List components = ClasspathUtils.getClasspathComponents();
        Collections.sort(components);
        assertTrue(Collections.binarySearch(components, file.getPath()) < 0);
        // test my package is found
        URLClassLoader ucl = new URLClassLoader(urls, classLoader);
        Thread.currentThread().setContextClassLoader(ucl);
        components = ClasspathUtils.getClasspathComponents();
        Collections.sort(components);
        assertTrue( Collections.binarySearch(components, file.getAbsolutePath()) >= 0);
    }
    finally
    {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:24,代码来源:ClasspathUtilsTest.java

示例4: setupProxy

import java.io.File; //导入方法依赖的package包/类
/**
 * The ProxyServer class is compiled by jtreg, but we want to
 * move it so it is not on the application claspath. We want to
 * load it through a separate classloader so that it has a separate
 * protection domain and security permissions.
 *
 * Its permissions are in the second grant block in each policy file
 */
static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
    testclasses = System.getProperty("test.classes");
    subdir = new File (testclasses, "proxydir");
    subdir.mkdir();

    movefile("ProxyServer.class");
    movefile("ProxyServer$Connection.class");
    movefile("ProxyServer$1.class");

    URL url = subdir.toURL();
    System.out.println("URL for class loader = " + url);
    URLClassLoader urlc = new URLClassLoader(new URL[] {url});
    proxyClass = Class.forName("ProxyServer", true, urlc);
    proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:Security.java

示例5: main

import java.io.File; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
    // create files from given arguments and tools.jar
    File f1 = new File(args[0]);
    String home = System.getProperty("java.home");
    String tools = ".." + File.separatorChar + "lib" +
        File.separatorChar + "tools.jar";
    File f2 = (new File(home, tools)).getCanonicalFile();

    // create class loader
    URL[] urls = { f1.toURL(), f2.toURL() };
    URLClassLoader cl = new URLClassLoader(urls);

    // load ListConnectors using the class loader
    // and then invoke the list method.
    Class c = Class.forName("ListConnectors", true, cl);
    Method m = c.getDeclaredMethod("list");
    Object o = c.newInstance();
    m.invoke(o);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:JdiLoadedByCustomLoader.java

示例6: genericNashornTest

import java.io.File; //导入方法依赖的package包/类
public Double genericNashornTest(final String benchmark, final String testPath, final String[] args) throws Throwable {
    try {
        final PerformanceWrapper wrapper = new PerformanceWrapper();

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PrintStream ps = new PrintStream(baos);

        final java.io.File test=new java.io.File(testPath);
        final File absoluteFile=test.getAbsoluteFile();
        @SuppressWarnings("deprecation")
        final
        URL testURL=absoluteFile.toURL();

        wrapper.runExecuteOnlyTest(testPath, 0, 0, testURL.toString(), ps, System.err, args);

        final byte[] output = baos.toByteArray();
        final List<String> result = outputToStrings(output);

        final Double _result = filterBenchmark(result, benchmark);

        return _result;
    } catch (final Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:OctaneTest.java

示例7: readBytes

import java.io.File; //导入方法依赖的package包/类
private static byte[] readBytes(File file) throws IOException {
  int byteCount = (int) file.length();

  byte[] input = new byte[byteCount];

  URL url = file.toURL();
  assertThat(url).isNotNull();

  InputStream is = url.openStream();
  assertThat(is).isNotNull();

  BufferedInputStream bis = new BufferedInputStream(is);
  int bytesRead = bis.read(input);
  bis.close();

  assertThat(bytesRead).isEqualTo(byteCount);
  return input;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:19,代码来源:StatUtils.java

示例8: load

import java.io.File; //导入方法依赖的package包/类
/**
 * Initialize any plugins in the given jar.
 * 
 * @param jar
 *            Jar containing plugin classes.
 * @param classes
 *            Set of all classes within the jar.
 * @throws Exception
 *             Thrown if URLClassLoader couldn't be made or an instance of a
 *             class could not be loaded.
 */
@SuppressWarnings("deprecation")
private void load(File jar, Set<String> classes) throws IOException {
	// Create loader, and load classes.
	URLClassLoader child = new URLClassLoader(new URL[] { jar.toURL() }, ClassLoader.getSystemClassLoader());
	for (String name : classes) {
		try {
			Class<?> loaded = Class.forName(name, true, child);
			// Load and register plugins.
			if (Plugin.class.isAssignableFrom(loaded)) {
				Plugin instance = (Plugin) loaded.newInstance();
				loadPlugin(instance);
			}
		} catch (Throwable e) {
			Recaf.INSTANCE.logging.info("From '" + jar.getName() + "' failed to load '" + name + "' because: " + e.toString(), 2);
		}
	}
}
 
开发者ID:Col-E,项目名称:Recaf,代码行数:29,代码来源:Plugins.java

示例9: readBytes

import java.io.File; //导入方法依赖的package包/类
private byte[] readBytes(File file) throws MalformedURLException, IOException {
  int byteCount = (int) file.length();

  byte[] input = new byte[byteCount];

  URL url = file.toURL();
  assertNotNull(url);

  InputStream is = url.openStream();
  assertNotNull(is);

  BufferedInputStream bis = new BufferedInputStream(is);
  int bytesRead = bis.read(input);
  bis.close();

  assertEquals(byteCount, bytesRead);
  return input;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:19,代码来源:StatArchiveWriterReaderIntegrationTest.java

示例10: setUp

import java.io.File; //导入方法依赖的package包/类
protected void setUp () throws Exception {
    
    cnt = 0;
    
    File f = new File(getWorkDir(), "layer.xml");
    FileWriter w = new FileWriter(f);
    w.write("<filesystem><file name='x.instance'> ");
    w.write("  <attr name='name' methodvalue='" + InstanceDataObjectGetNameTest.class.getName() + ".computeName'/> ");
    w.write("</file></filesystem> ");
    w.close();

    fs = new MultiFileSystem(new FileSystem[] { 
        FileUtil.createMemoryFileSystem(), 
        new XMLFileSystem(f.toURL())
    });
    FileObject fo = fs.findResource("x.instance");
    assertNotNull(fo);
    
    assertNull(fo.getAttribute("name"));
    assertEquals("One call", 1, cnt);
    // clean
    cnt = 0;

    obj = DataObject.find(fo);
    
    assertEquals("No calls now", 0, cnt);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:28,代码来源:InstanceDataObjectGetNameTest.java

示例11: preferFileURL

import java.io.File; //导入方法依赖的package包/类
/**
 * If possible it finds "file:" URL if <code>fileObject</code> is on LocalFileSystem.
 * @return URL of <code>fileObject</code>.
 */
protected URL preferFileURL(FileObject fileObject) throws MalformedURLException, FileStateInvalidException {
    URL fileURL = null;
    File file = FileUtil.toFile(fileObject);
    
    if ( file != null ) {
        fileURL = file.toURL();
    } else {
        fileURL = fileObject.getURL();
    }
    return fileURL;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:TransformPerformer.java

示例12: go

import java.io.File; //导入方法依赖的package包/类
static void go(String fn) throws Exception {
    File f = new File(fn);
    URL u = f.toURL();
    String ufn = u.getFile();
    if (!ufn.endsWith("/"))
        throw new Exception(u + " does not end with slash");
    if (ufn.endsWith("//"))
        throw new Exception(u + " ends with two slashes");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ToURL.java

示例13: PluginBubble

import java.io.File; //导入方法依赖的package包/类
public PluginBubble(File pluginFile) throws Exception {
    URLClassLoader loader = new URLClassLoader(new URL[]{pluginFile.toURL()}, ClassLoader.getSystemClassLoader());
    InputStream stream = loader.getResourceAsStream("plugin.json");
    JSONObject json = new JSONObject(IOUtils.toString(stream, "utf-8"));
    stream.close();

    pluginName = json.getString("name");
    pluginInstance = (SagiriPlugin) loader.loadClass(json.getString("main")).newInstance();
}
 
开发者ID:kazigk,项目名称:sagiri,代码行数:10,代码来源:PluginBubble.java

示例14: findResource

import java.io.File; //导入方法依赖的package包/类
/**
 * Finds a resource. This method is called by {@code getResource()} after
 * the parent ClassLoader has failed to find a loaded resource of the same
 * name.
 *
 * @param name The name of the resource to find
 * @return the location of the resource as a URL, or {@code null} if the
 * resource is not found.
 */
@Override
protected URL findResource(String name) {
    ensureInit();

    int length = mFiles.length;

    for (int i = 0; i < length; i++) {
        File pathFile = mFiles[i];
        ZipFile zip = mZips[i];

        if (zip.getEntry(name) != null) {
            if (VERBOSE_DEBUG)
                System.out.println("  found " + name + " in " + pathFile);
            try {
                // File.toURL() is compliant with RFC 1738 in always
                // creating absolute path names. If we construct the
                // URL by concatenating strings, we might end up with
                // illegal URLs for relative names.
                return new URL("jar:" + pathFile.toURL() + "!/" + name);
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    if (VERBOSE_DEBUG)
        System.out.println("  resource " + name + " not found");

    return null;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:40,代码来源:DexClassLoader.java

示例15: main

import java.io.File; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
    // create files from given arguments and tools.jar
    File f1 = new File(args[0]);

    // create class loader
    URL[] urls = { f1.toURL() };
    URLClassLoader cl = new URLClassLoader(urls);

    // load ListConnectors using the class loader
    // and then invoke the list method.
    Class c = Class.forName("ListConnectors", true, cl);
    Method m = c.getDeclaredMethod("list");
    Object o = c.newInstance();
    m.invoke(o);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:JdiLoadedByCustomLoader.java


注:本文中的java.io.File.toURL方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。