Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Eclipse中引用不在src中的文件_Java_Eclipse_Path - Fatal编程技术网

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

Java 如何在Eclipse中引用不在src中的文件,java,eclipse,path,Java,Eclipse,Path,我想为MyBatis找个资源。本教程指出,在我的连接工厂中,我需要以下内容: String resource = "org/mybatis/example/Configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlMapper = new SqlSessionFactoryBuilder().build(reader); 我的目录结构是: src/ com/ 乌提尔斯/ MyBatisConn

我想为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”


有人知道怎么做吗?

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


因此,您的配置文件将位于类路径的根目录下,并且可以通过
getClass().getResourceAsStream(“/configuration.xml”)
而不是
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);

你会不会碰巧知道如何使用MyEclipse?MyEclipse只是eclipse的一个补充,所以它是一样的。