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