writeAppend.jsp
$#@60;html$#@62; $#@60;head$#@62; $#@60;title$#@62;追加数据$#@60;/title$#@62; $#@60;/head$#@62; $#@60;body bgcolor="#000000"$#@62; $#@60;%--创建javabean并设置属性 --%$#@62; $#@60;jsp:useBean id="writer" class="WriteAppend" scope="request"$#@62; $#@60;jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" /$#@62; $#@60;jsp:setProperty name="writer" property="something" value="初始化something属性" /$#@62; $#@60;/jsp:useBean$#@62;
$#@60;h3$#@62;追加数据$#@60;/h3$#@62;
$#@60;p$#@62; $#@60;%--设置要追加的字符串 --%$#@62; $#@60;% writer.setSomething("追加数据"); %$#@62; $#@60;%--读取上面设置的字符串 --%$#@62; $#@60;% out.print(writer.getSomething()); %$#@62; $#@60;%--调用writer的writeSomething方法追加文件并返回成功或者出错信息 --%$#@62; $#@60;% out.print(writer.writeSomething()); %$#@62;
$#@60;/p$#@62; $#@60;/body$#@62; $#@60;/html$#@62;
//WriteAppend.java javabean文件 import java.io.*;
public class WriteAppend { private String path;//文件路径 private String something;//追加的字符串变量 //初始化 public WriteAppend() { path = null; something = "Default message"; } //设置文件路径 public void setPath(String apath) { path = apath; } //得到文件路径 public String getPath() { return path; } //设置要追加的字符串 public void setSomething(String asomething) { something = asomething; } //得到要追加的字符串 public ring getSomething() { return something; } //追加字符串 public String writeSomething() { try { //创建文件path并写入something字符串,注意和写入篇的区别 FileWriter theFile = new FileWriter(path,true); PrintWriter out = new PrintWriter(theFile); out.print(something + "\n"); out.close(); //关闭文件并返回success字符串 theFile.close(); return "success!!"; } catch (IOException e) { return e.toString(); } } }
好了,到此文件操作的全部内容都完成了,如果您看到这里,相信您对文件基本操作已经OK了。
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18