解决java.io.FileNotFoundException: class path resource [test.txt] cannot be opened because it does not

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/q1406689423/article/details/82849272

今天使用IDEA的时候遇到一个异常

java.io.FileNotFoundException: class path resource [test.txt] cannot be opened because it does not exist

但是反复检查很多遍,确实是有这个文件的,我在代码里用的是

classpath:com/xmh/test.txt

难道是因为加了classpath?去掉classpath还是不行啊!用绝对路径试一下还是不可以。就在网上找问题,确实有和我一样的问题的!

才知道原来是IDEA的maven项目默认情况下是不将xml、properties等类型的文件放在编译后的class文件夹中的。看了一下Target文件夹,确实是只有class文件,没有见到其他定义后的文件,那要怎么办呢?难道啊每次都要手动将文件放在这个文件夹吗?
查了一下,原来maven本身就有这个,在maven中添加以下代码就可以将文件放在编译后的class文件夹了!

        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.txt</include>
                </includes>
            </resource>
        </resources>

这样就是把后缀为.txt的文件放进去了!这问题虽然不是大问题,但也挺烦人的,在这里记录一下,希望可以帮到有需要的人!

猜你喜欢

转载自blog.csdn.net/q1406689423/article/details/82849272