java.lang.ClassLoader.getResourceAsStream()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(185)

本文整理了Java中java.lang.ClassLoader.getResourceAsStream()方法的一些代码示例,展示了ClassLoader.getResourceAsStream()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassLoader.getResourceAsStream()方法的具体详情如下:
包路径:java.lang.ClassLoader
类名称:ClassLoader
方法名:getResourceAsStream

ClassLoader.getResourceAsStream介绍

[英]Returns a stream for the resource with the specified name. See #getResource(String) for a description of the lookup algorithm used to find the resource.
[中]返回具有指定名称的资源的流。有关用于查找资源的查找算法的说明,请参见#getResource(String)。

代码示例

代码示例来源:origin: jenkinsci/jenkins

/**
 * Finds a system resource (which should be loaded from the parent
 * classloader).
 *
 * @param name The name of the system resource to load.
 *             Must not be <code>null</code>.
 *
 * @return a stream to the named resource, or <code>null</code> if
 *         the resource cannot be found.
 */
private InputStream loadBaseResource(String name) {
  return parent == null ? super.getResourceAsStream(name) : parent.getResourceAsStream(name);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public InputStream getResourceAsStream(String name) {
  return this.enclosingClassLoader.getResourceAsStream(name);
}

代码示例来源:origin: stackoverflow.com

// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader()
                .getResourceAsStream("SomeTextFile.txt");
// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

代码示例来源:origin: stackoverflow.com

package dummy;

import java.io.*;

public class Test
{
  public static void main(String[] args)
  {
    InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
    System.out.println(stream != null);
    stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
    System.out.println(stream != null);
  }
}

代码示例来源:origin: stackoverflow.com

ClassLoader classLoader = getClass().getClassLoader();
InputStream input = classLoader.getResourceAsStream("/com/example/foo.properties");
// ...

代码示例来源:origin: stackoverflow.com

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("com/example/foo.properties");
// ...

代码示例来源:origin: stackoverflow.com

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("foo.properties");
// ...
Properties properties = new Properties();
properties.load(input);

代码示例来源:origin: alibaba/druid

private InputStream getFileAsStream(String filePath) throws FileNotFoundException {
    InputStream inStream = null;
    File file = new File(filePath);
    if (file.exists()) {
      inStream = new FileInputStream(file);
    } else {
      inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
    }
    return inStream;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Open an InputStream for the specified class.
 * <p>The default implementation loads a standard class file through
 * the parent ClassLoader's {@code getResourceAsStream} method.
 * @param name the name of the class
 * @return the InputStream containing the byte code for the specified class
 */
@Nullable
protected InputStream openStreamForClass(String name) {
  String internalName = name.replace('.', '/') + CLASS_FILE_SUFFIX;
  return getParent().getResourceAsStream(internalName);
}

代码示例来源:origin: eclipse-vertx/vert.x

public static void loadConfig() {
 try (InputStream is = JULLogDelegateFactory.class.getClassLoader().getResourceAsStream("vertx-default-jul-logging.properties")) {
  if (is != null) {
   LogManager.getLogManager().readConfiguration(is);
  }
 } catch (IOException ignore) {
 }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  public InputStream getResourceAsStream(final String name) {
    if (name.endsWith(ClassUtils.CLASS_EXTENSION)) {
      String qualifiedClassName = name.substring(0, name.length() - ClassUtils.CLASS_EXTENSION.length()).replace('/', '.');
      JavaFileObjectImpl file = (JavaFileObjectImpl) classes.get(qualifiedClassName);
      if (file != null) {
        return new ByteArrayInputStream(file.getByteCode());
      }
    }
    return super.getResourceAsStream(name);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  public InputStream getResourceAsStream(final String name) {
    if (name.endsWith(ClassUtils.CLASS_EXTENSION)) {
      String qualifiedClassName = name.substring(0, name.length() - ClassUtils.CLASS_EXTENSION.length()).replace('/', '.');
      JavaFileObjectImpl file = (JavaFileObjectImpl) classes.get(qualifiedClassName);
      if (file != null) {
        return new ByteArrayInputStream(file.getByteCode());
      }
    }
    return super.getResourceAsStream(name);
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

public String getVersion() {
 try (InputStream is = getClass().getClassLoader().getResourceAsStream("META-INF/vertx/vertx-version.txt")) {
  if (is == null) {
   throw new IllegalStateException("Cannot find vertx-version.txt on classpath");
  }
  try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) {
   return scanner.hasNext() ? scanner.next() : "";
  }
 } catch (IOException e) {
  throw new IllegalStateException(e.getMessage());
 }
}

代码示例来源:origin: ctripcorp/apollo

@Override
public void initialize() {
 try {
  InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(APP_PROPERTIES_CLASSPATH);
  if (in == null) {
   in = DefaultApplicationProvider.class.getResourceAsStream(APP_PROPERTIES_CLASSPATH);
  }
  initialize(in);
 } catch (Throwable ex) {
  logger.error("Initialize DefaultApplicationProvider failed.", ex);
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a new {@link WebSphereClassPreDefinePlugin}.
 * @param transformer the {@link ClassFileTransformer} to be adapted
 * (must not be {@code null})
 */
public WebSphereClassPreDefinePlugin(ClassFileTransformer transformer) {
  this.transformer = transformer;
  ClassLoader classLoader = transformer.getClass().getClassLoader();
  // First force the full class loading of the weaver by invoking transformation on a dummy class
  try {
    String dummyClass = Dummy.class.getName().replace('.', '/');
    byte[] bytes = FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream(dummyClass + ".class"));
    transformer.transform(classLoader, dummyClass, null, null, bytes);
  }
  catch (Throwable ex) {
    throw new IllegalArgumentException("Cannot load transformer", ex);
  }
}

代码示例来源:origin: alibaba/druid

public static byte[] readByteArrayFromResource(String resource) throws IOException {
  InputStream in = null;
  try {
    in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    if (in == null) {
      return null;
    }
    return readByteArray(in);
  } finally {
    JdbcUtils.close(in);
  }
}

代码示例来源:origin: apache/kafka

public static Object deserialize(String fileName) throws IOException, ClassNotFoundException {
    ClassLoader classLoader = Serializer.class.getClassLoader();
    InputStream fileStream = classLoader.getResourceAsStream(fileName);
    return deserialize(fileStream);
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testGetResourceAsStream(long ver, ClassLoader cl) throws Exception {
 try (InputStream is = cl.getResourceAsStream(resourceName)) {
  assertNotNull(is);
  try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) {
   assertTrue(scanner.hasNext());
   JsonObject json = new JsonObject(scanner.next());
   assertEquals(ver, json.getLong("ver", -1L).longValue());
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testFindsExistingResourceWithGetResourceAsStreamAndNoOverrides() {
  assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
  assertNotNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testDoesNotFindExistingResourceWithGetResourceAsStreamAndNullOverride() {
  assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
  overridingLoader.override(EXISTING_RESOURCE, null);
  assertNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
}

相关文章