我写了个例子,不用RandomAccessFile也可以.
import java.io.*;
public class writelog {
public static void main(String[] args) {
try{
FileWriter fw=new FileWriter("c:\\aa.txt",true);
BufferedWriter rf1=new BufferedWriter(fw);
for (int i = 0; i < 100; i ++ ){
rf1.write("this is line "+i);
rf1.newLine();
rf1.flush();
Thread.sleep(1000);
}
rf1.close();
}catch (Exception e){
System.out.println("write error!");
}
import java.io.BufferedReader;
import java.io.FileReader;
public class readlog {
public static void main(String[] args) {
try{
BufferedReader rf1 = new BufferedReader(new FileReader("c:\\aa.txt"));
String s1=null;
while((s1=rf1.readLine())!=null){
System.out.println(s1);
Thread.sleep(1000);
}
rf1.close();
}catch (Exception e){
System.out.println("read error!");
}
}
}
writelog是模拟连续写LOG内容部分, readlog 是读取不断更新的LOG文件部分, 先执行writelog,再执行readlog .