如何在Eclipse中引用不在src中的文件

发布于 2024-10-05 10:10:04 字数 476 浏览 0 评论 0原文

我正在尝试获取 MyBatis 的资源。该教程指出,我的连接工厂中需要以下内容:

String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);

我的目录结构是:

src/ com/ 实用程序/ MyBatisConnectionFactory.java 配置/ Configuration.xml

我在引用配置文件时遇到问题。我尝试了“config/Configuration.xml”、“Configuration.xml”和“/config/Configuration.xml”。

有人知道该怎么做吗?

I'm trying to get a resource for MyBatis. The tutorial states that I will need the following in my Connection Factory:

String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);

My directory structure is:

src/
com/
utils/
MyBatisConnectionFactory.java
config/
Configuration.xml

I am having troubles referencing the configuration file. I tried "config/Configuration.xml", "Configuration.xml" and "/config/Configuration.xml".

Anyone have a good idea for what to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-10-12 10:10:04

您可以将 config 目录添加为源文件夹(右键单击 > 构建路径 > 用作源文件夹)。

因此,您的配置文件将位于类路径的根目录中,并且可以通过 getClass().getResourceAsStream("/Configuration.xml") 访问

You can add your config directory as a source-folder (right-click > build path > use as source folder).

Thus your configuration files will go on the root of the classpath and will be accessible via getClass().getResourceAsStream("/Configuration.xml")

呆萌少年 2024-10-12 10:10:04

使用 getResourcesAsStream() 而不是 Resources.getResourceAsReader() 通过类路径打开文件 例如:

InputStream is = getClass().getClassLoader().getResourceAsStream(
    "src/com/utils/Configuration.xml");
byte[] data = new byte[is.available()];
is.read(data);
is.close();
String fileContents = new String(data);

Open up the file via the classpath using getResourcesAsStream() rather than Resources.getResourceAsReader() For example:

InputStream is = getClass().getClassLoader().getResourceAsStream(
    "src/com/utils/Configuration.xml");
byte[] data = new byte[is.available()];
is.read(data);
is.close();
String fileContents = new String(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文