RamdonAccessFile可以的,并且可以实现文件的追加.下面是一个Exampleimport 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){}
}
}
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){}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货