【发布时间】:2017-08-04 07:58:35
【问题描述】:
我的 Eclipse 中有以下项目结构。
我在 servlet 中的代码如下。
File entityFile = new File(getServletContext().getContextPath() + "/EntityList/entities.txt");
FileWriter fout = new FileWriter(entityFile);
fout.write("The Content");
fout.close();
这里基本上我正在尝试写入/EntityList/entities.txt 上的可用文件,但是当我运行它时,我得到如下异常。
严重:Servlet.service() 用于 servlet [com.luis.servlets.WriteEntityToAFile] 在上下文中的路径 [/LUISWebUI] 抛出异常 java.io.FileNotFoundException: \LUISWebUI\EntityList\entities.txt(系统找不到路径 指定)
我知道我走错了路,有人可以帮我指明正确的方向吗。
更新
为混乱道歉。
我正在将一些数据从 jsp 发送到 servlet 以写入 entities.txt 文件。我能够在 servlet 中捕获它(通过 sysout 进行交叉检查)。
【问题讨论】:
-
这个
/EntityList/entities.txt在部署的服务器上的什么位置? -
将
EntitiesList放入 WebContent` 文件夹并使用 InputStream stream = getServletContext().getResourceAsStream("/EntitiesList/entities.txt"); -
@Ramanlfc,那么我将如何写入entities.txt,抱歉造成混淆,更新了我的问题。