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

📄 利用ramdonaccessfile来实现文件的追加!.txt

📁 我自自己在学习过程找到的一些jsp代码
💻 TXT
字号:
作者:jeru
日期:2000-11-29 14:44:48
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){}
}
}

⌨️ 快捷键说明

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