简体   繁体   English

相对路径文件未找到异常

[英]Relative path File not found exception

I am working on Simple web application in netbeans where i am getting FileNotFound Exception. 我正在netbeans中处理简单的Web应用程序,我正在获取FileNotFound Exception。 I have stored files in class path so i need to use relative paths, when i tried with absulate path its working fine for me. 我已经在类路径中存储了文件,所以我需要使用相对路径,当我尝试使用absulate path时,它对我来说很好。

Below image shows my File System hierarchy . 图显示了我的文件系统层次结构

在此输入图像描述

I need to write content data in file DBList.txt. 我需要在文件DBList.txt中写入内容数据。

My Code is , 我的代码是

File file = new File("data/application/DBList.txt");
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));

I have searched lot but not getting solution for reading file using relative path. 我搜索了很多但没有获得使用相对路径读取文件的解决方案。

The path is relative to the working directory of the server , not your project in NetBeans. 该路径相对于服务器的工作目录,而不是NetBeans中的项目。 Given your FNFE I suspect that the directory structure data/application/ doesn't exist under the working directory. 鉴于您的FNFE我怀疑目录结构data/application/不存在于工作目录下。

What server are you running and how are you starting it? 你在运行什么服务器,你是如何启动它的? You can figure out the server's working directory by logging; 您可以通过记录来确定服务器的工作目录;

File wd = new File(".");
log.debug("working dir: " + wd.getAbsolutePath());

Edit: 编辑:
The File class and the classpath are totally unrelated concepts. File类和类路径是完全不相关的概念。 Don't confuse the two. 不要混淆两者。 If you are looking to use classpath resources have a look at the getResource() method in ClassLoader . 如果您正在寻找使用的classpath资源具有看看getResource()中的方法ClassLoader

尝试使用“\\\\”而不是“/”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM