您现在的位置:软界网技术中心WEB开发JSP > 技术显示
利用RamdonAccessFile来实现文件的追加
2002-6-28 12:55:00   网友评论       阅读次数 点此评论
   RamdonAccessFile 是个很好用的类,功能十分强大,可以利用它的length()和seek()方法来轻松实现文件的追加,相信我下面这个例子是很容易看懂的,先写入十行,用length()读出长度(以byte为单位),在用seek()移动到文件末尾,继续添加,最后显示记录。

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){}
}
}

相关文章
最新更新
文章阅读排行
周排行
月排行
欢迎订阅天极网RSS聚合资讯:http://www.yesky.com/index.xml    
 
      来源: 作者:
 
【评论查看】