使用记事本的FileNotFoundException [英] FileNotFoundException using Notepad

查看:77
本文介绍了使用记事本的FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Windows Prompt(控制台)安装和运行的Maven项目.每次我运行它时,我都会得到一个FileNotFoundException:

I have a maven project which I install and run using Windows Prompt (console). And every time I run it I get a FileNotFoundException:

public Properties getConfigProperties() throws Exception {
        ClassLoader loader = getClass().getClassLoader();
        File file = new File(loader.getResource("config.properties").getFile());
        config.load(new FileReader(file));
        return config;
}

或者NullPointerException(如果我使用classpath:config.properties).

config.properties文件位于 /src/main/resources/ 文件夹中.

config.properties file is inside /src/main/resources/ folder.

如果我在IDE中运行项目,它也会成功完成,不会发生任何异常.

Also if I run the project in the IDE it completes successfully without any exceptions occurring.

那么当我使用console命令时会出什么问题?

So what's the problem when I use the console command?

推荐答案

File file = new File(loader.getResource("config.properties").getFile());

使用

File file = new File(loader.getResourceAsStream("config.properties"));

将XML文件加载到类路径中的另一种方法是使用Spring框架,如下所示:

Another way to get your xml file loaded in your classpath is by using the Spring Framework as below:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resolver.getResources("classpath*:your/package/**/config.properties");

这篇关于使用记事本的FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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