在jsp中写text文件.txt

来自「499篇java文章」· 文本 代码 · 共 29 行

TXT
29
字号
作者:jspfuns
日期:2000-8-9 11:34:09
作者:Blueski编译


使用PrintWriter对象即可写Text文件。

请参考以下示例: 

<%@ page import="java.io.*" %>
<%
String str = "print me";
//always give the path from root. This way it almost always works.
String nameOfTextFile = "/usr/anil/imp.txt";
try { 
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(str);
//clean up
pw.close();
} catch(IOException e) {
out.println(e.getMessage());
}
%>

现在,打开imp.txt加以查看,字符串"print me"应该已经写入。 

还有另一种方法,使用IN16标记库的file tag,可参考http://sourceforge.net/project/?group_id=1282.
其示例的语法为:
<ext:file action="write|append|create|copy|move|delete|rename|read" from="<%= myfilename %>">

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?