本文整理汇总了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();
}
示例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();
}
示例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);
}
}
示例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);
}
示例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);
}
示例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;
}
}
示例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;
}
示例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);
}
}
}
示例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;
}
示例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);
}
示例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;
}
示例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");
}
示例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();
}
示例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;
}
示例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);
}