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

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

问题描述

我想读取位于我的课程路径中的 jar 之一内的XML文件。如何读取任何包含在 jar 中的文件

解决方案

如果你想从你的应用程序中读取这个文件,请使用:
$ b $ pre $ InputStream input = getClass()。getResourceAsStream(/ classpath /到/我/文件);

路径以/开头,但不是文件系统中的路径,在你的类路径中。所以,如果你的文件在类路径org.xml中被称为myxml.xml,那么你的路径就像/org/xml/myxml.xml。

InputStream读取文件的内容。如果你愿意的话,你可以把它包装到Reader中。



我希望有帮助。


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");

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".

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

I hope that helps.

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

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