TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
<%@ page contentType="text/HTML;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>如何将数据追加写入到文件
</title>
</head>
<body>
<%
String path=
request.getRealPath("/article/article5a/example/filetest");
RandomAccessFile rf=
new RandomAccessFile(path + "\WriteData.txt","rw");
//定义一个类RandomAccessFile的对象,并实例化
rf.seek(rf.length());//将指针移动到文件末尾
rf.writeBytes("
Append a line to the file!");
rf.close();//关闭文件流
out.println("写入文件内容为:<br>");
FileReader fr=new FileReader(path + "\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//读取文件的BufferedRead对象
String Line=br.readLine();
while(Line!=null){
out.println(Line + "<br>");
Line=br.readLine();
}
fr.close();//关闭文件
%>
</body>
</html>
源码下载:http://file.javaxxz.com/2014/10/1/015400094.zip |
|