⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0164.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>利用RamdonAccessFile来实现文件的追加</strong></big></p>
<div align="right">---摘自互联网</div>

<br>RamdonAccessFile&nbsp; 是个很好用的类,功能十分强大,可以利用它的<br>
length()和seek()方法来轻松实现文件的追加,相信我下面这个例子是<br>
很容易看懂的,先写入十行,用length()读出长度(以byte为单位),<br>
在用seek()移动到文件末尾,继续添加,最后显示记录。<br>
<br>
import&nbsp; java.io.*;<br>
public&nbsp; class&nbsp; IOStreamDemo&nbsp; {<br>
public&nbsp; static&nbsp; void&nbsp; main(String[]&nbsp; args)&nbsp; {<br>
try{<br>
RandomAccessFile&nbsp; rf1&nbsp; =&nbsp; new&nbsp; RandomAccessFile("d:\\jeru.txt","rw");<br>
for&nbsp; (int&nbsp; i&nbsp; =&nbsp; 0;&nbsp; i&nbsp; &lt;&nbsp; 10;&nbsp; i&nbsp; ++&nbsp; )&nbsp; {<br>
rf1.writeBytes("xixi,this&nbsp; is&nbsp; line&nbsp; "+i+"\n");<br>
}<br>
rf1.close();<br>
<br>
int&nbsp; i&nbsp; =&nbsp; 0;<br>
String&nbsp; record&nbsp; =&nbsp; new&nbsp; String();<br>
RandomAccessFile&nbsp; rf2&nbsp; =&nbsp; new&nbsp; RandomAccessFile("d:\\jeru.txt","rw");<br>
rf2.seek(rf2.length());<br>
rf2.writeBytes("lala,append&nbsp; line"+"\n");<br>
rf2.close();<br>
<br>
RandomAccessFile&nbsp; rf3&nbsp; =&nbsp; new&nbsp; RandomAccessFile("d:\\jeru.txt","r");<br>
while&nbsp; ((record&nbsp; =&nbsp; rf3.readLine())&nbsp; !=&nbsp; null)&nbsp; {<br>
i&nbsp; ++;<br>
System.out.println("Value&nbsp; "+i+":"+record);<br>
}<br>
rf3.close();<br>
}catch(Exception&nbsp; e){}<br>
}<br>
}

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

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