【发布时间】:2011-12-09 14:05:58
【问题描述】:
我想从我的过滤器写入一个文件,然后能够在 Eclipse 中读取它以确保我已正确写入。
这段代码编译并运行良好,但我不知道我可以去哪里读取文件,或者我是否写过任何东西。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println("filter invoked");
InputStream in = null;
OutputStream out = null;
String inPath = context.getRealPath("/WEB-INF/template.txt");
String outPath = context.getRealPath("/WEB-INF/output.txt");
in = new FileInputStream(inPath);
out = new FileOutputStream(outPath);
OutputStreamWriter outstream = new OutputStreamWriter(out);
outstream.write("hello");
// pass the request along the filter chain
chain.doFilter(request, response);
}
文件 template.txt 和 output.txt 位于 WEB-INF 目录中。我已经验证从文件中读取工作正常,但我无法验证对它们的写入。每次我写入 output.txt 时,文件仍然没有变化。
关于在 Web 应用程序环境中写入文件,我有什么不明白的地方?
【问题讨论】:
标签: java eclipse web-applications servlet-filters