相对路径文件未找到异常

Ani*_*ket 1 java relative-path filenotfoundexception

我正在netbeans中处理简单的Web应用程序,我正在获取FileNotFound Exception.我已经在类路径中存储了文件,所以我需要使用相对路径,当我尝试使用absulate path时,它对我来说很好.

图显示了我的文件系统层次结构.

在此输入图像描述

我需要在文件DBList.txt中写入内容数据.

我的代码是,

File file = new File("data/application/DBList.txt");
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
Run Code Online (Sandbox Code Playgroud)

我搜索了很多但没有获得使用相对路径读取文件的解决方案.

Qwe*_*rky 5

该路径相对于服务器的工作目录,而不是NetBeans中的项目.鉴于您的FNFE,我怀疑data/application/工作目录下不存在目录结构.

你在运行什么服务器,你是如何启动它的?您可以通过记录来确定服务器的工作目录;

File wd = new File(".");
log.debug("working dir: " + wd.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)

编辑:
File类和类路径是完全无关的概念.不要混淆两者.如果您正在寻找使用classpath资源,请查看该getResource()方法ClassLoader.