简体   繁体   English

从 jar 中获取 csv 文件

[英]get csv file from jar

I have the following line of code:我有以下代码行:

InputStreamReader isr = new InputStreamReader(MethodHandles.lookup().lookupClass().getResourceAsStream(csvFile));

could someone explain to a newbe:有人可以向新手解释一下:

MethodHandles.lookup()
lookupClass()
getResourceasStream()

the code works and accesses a csv file located in a jar.该代码工作并访问位于 jar 中的 csv 文件。 I just don't understand what each of the methods is doing我只是不明白每种方法在做什么

I was able to simplify the line to:我能够将这条线简化为:

InputStreamReader isr = new InputStreamReader (SQLUtilPROD.class.getResourceAsStream (csvFile));

but still confused.但还是很困惑。 What does SQLUtilProd.class do? SQLUtilProd.class 有什么作用? and how does getResourceAsStream know to get the file from the jar?以及 getResourceAsStream 如何知道从 jar 中获取文件? What happens if you have multiple jars?如果你有多个 jar 会发生什么?

Not sure but I think we use SQLUtil.class to get the class object which in turns gives us access to classLoader which getResourceAsStream uses to locate the file.不确定,但我认为我们使用 SQLUtil.class 来获取类对象,这反过来又让我们可以访问 getResourceAsStream 用来定位文件的 classLoader。

If that's true where does classLoader define the path to include the jar?如果这是真的,classLoader 在哪里定义包含 jar 的路径?

What does SQLUtilProd.class do? SQLUtilProd.class 有什么作用?

The data for each object contains a reference to an object of class java.lang.Class, and this is returned by the method getClass.每个对象的数据都包含对 java.lang.Class 类对象的引用,该引用由方法 getClass 返回。 There is also one java.lang.Class object describing java.lang.Class.还有一个 java.lang.Class 对象描述 java.lang.Class。 Please read more in this link.请在链接中阅读更多内容。

how does getResourceAsStream know to get the file from the jar? getResourceAsStream 如何知道从 jar 中获取文件?

getResourceAsStream will find a resource on the classpath with a given name. getResourceAsStream将在类路径上找到具有给定名称的资源。 The rules for searching resources associated with a given class are implemented by the defining class loader of the class.搜索与给定类相关联的资源的规则由的定义类加载器实现。 Please visit this link.请访问链接。

What happens if you have multiple jars?如果你有多个 jar 会发生什么?

You can get all resources with a helper function like this:您可以使用这样的辅助函数获取所有资源:

public static List<InputStream> loadResources(
        final String name, final ClassLoader classLoader) throws IOException {
    final List<InputStream> list = new ArrayList<InputStream>();
    final Enumeration<URL> systemResources = 
            (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
            .getResources(name);
    while (systemResources.hasMoreElements()) {
        list.add(systemResources.nextElement().openStream());
    }
    return list;
}

I'm sure that reading this link will help you.我相信阅读此链接会对您有所帮助。

where does classLoader define the path to include the jar? classLoader 在哪里定义包含 jar 的路径?

The system ClassLoader provides access to information in the CLASSPATH .系统 ClassLoader 提供对CLASSPATH 中信息的访问。 A CLASSPATH may contain directories and JAR files. CLASSPATH 可能包含目录和 JAR 文件。 So you have access to all resources of your project and jar files which they are in the classpath.因此,您可以访问项目的所有资源和类路径中的 jar 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM