如何从Java中的jar读取文件? [英] How to read a file from jar in Java?

查看:31
本文介绍了如何从Java中的jar读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取位于我的类路径中包含的 jar 之一内的 XML 文件.如何读取 jar 中包含的任何文件?

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?

推荐答案

如果您想从应用程序内部读取该文件,请使用:

If you want to read that file from inside your application use:

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");

路径以/"开头,但这不是文件系统中的路径,而是类路径中的路径.因此,如果您的文件位于类路径org.xml"并且名为 myxml.xml,则您的路径看起来像/org/xml/myxml.xml".

The path starts with "/", but that is not the path in your file-system, but in your classpath. So if your file is at the classpath "org.xml" and is called myxml.xml your path looks like "/org/xml/myxml.xml".

InputStream 读取文件的内容.如果需要,您可以将其包装到 Reader 中.

The InputStream reads the content of your file. You can wrap it into an Reader, if you want.

希望能帮到你.

这篇关于如何从Java中的jar读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆