当前页面: 开发资料首页 → Java 专题 → Java文件操作大全8:如何将数据追加写入到文件
Java文件操作大全8:如何将数据追加写入到文件
摘要: Java文件操作大全8:如何将数据追加写入到文件
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle"> <table width="731" border="0">
<tr>
<td width="725">
</td>
</tr>
</table>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<head>
如何将数据追加写入到文件
</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("\nAppend a line to the file!");
rf.close();//关闭文件流
out.println("写入文件内容为:
");
FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//读取文件的BufferedRead对象
String Line=br.readLine();
while(Line!=null){
out.println(Line + "
");
Line=br.readLine();
}
fr.close();//关闭文件
%>
</body>
</td>
</tr>
<tr>
↑返回目录
前一篇: Java文件操作大全6:略过字节不读取
后一篇: Java文件操作大全7:将写入文件的数据分行