读取文件:
ServletContext application = ...;InputStream in = null;try { in = application.getResourceAtStream("/WEB-INF/web.xml"); // example // read your file} finally { if(null != in) try { in.close(); } catch (IOException ioe) { }}写入文件:
ServletContext application = ...;File tmpdir = (File)application.getAttribute("javax.servlet.context.tempdir");if(null == tmpdir) throw new IllegalStateException("Container does not provide a temp dir"); // Or handle otherwiseFile targetFile = new File(tmpDir, "my-temp-filename.txt");BufferedWriter out = null;try { out = new BufferedWriter(new FileWriter(targetFile)); // write to output stream} finally { if(null != out) try { out.close(); } catch (IOException ioe) { }}如果您不想使用servlet容器提供的tmpdir,则应该使用完全不在servlet上下文所能提供的范围之内的类似之类的
/path/to/temporary/files东西。您绝对不希望将容器的临时目录用于除真正的临时文件以外的其他任何东西,这些文件可以在重新部署后删除,等等。



