?? 利用ramdonaccessfile來(lái)實(shí)現(xiàn)文件的追加!.txt
字號(hào):
作者:jeru
日期:2000-11-29 14:44:48
RamdonAccessFile 是個(gè)很好用的類(lèi),功能十分強(qiáng)大,可以利用它的
length()和seek()方法來(lái)輕松實(shí)現(xiàn)文件的追加,相信我下面這個(gè)例子是
很容易看懂的,先寫(xiě)入十行,用length()讀出長(zhǎng)度(以byte為單位),
在用seek()移動(dòng)到文件末尾,繼續(xù)添加,最后顯示記錄。
import java.io.*;
public class IOStreamDemo {
public static void main(String[] args) {
try{
RandomAccessFile rf1 = new RandomAccessFile("d:\\jeru.txt","rw");
for (int i = 0; i < 10; i ++ ) {
rf1.writeBytes("xixi,this is line "+i+"\n");
}
rf1.close();
int i = 0;
String record = new String();
RandomAccessFile rf2 = new RandomAccessFile("d:\\jeru.txt","rw");
rf2.seek(rf2.length());
rf2.writeBytes("lala,append line"+"\n");
rf2.close();
RandomAccessFile rf3 = new RandomAccessFile("d:\\jeru.txt","r");
while ((record = rf3.readLine()) != null) {
i ++;
System.out.println("Value "+i+":"+record);
}
rf3.close();
}catch(Exception e){}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -