展开全部
Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何地方。这就是“Random”的意62616964757a686964616fe58685e5aeb931333337613234义所在。
相关API:
RandomAccessFile(String
name, String
mode)构造器,模式分为r(只读),rw(读写)等
RandomAccessFile.readLine()方法实现对一整行的读取,并重新定位操作位置
RandomAccessFile.write(byte[] b)用于字节内容的写入
示例如下:RandomAccessFile raf = new RandomAccessFile("f:/1.txt", "rw");
int targetLineNum = 10;
int currentLineNum = 0;
while(raf.readLine() != null){
if(currentLineNum == targetLineNum){ // 定位到目标行时结束
break;
}
currentLineNum++;
}
raf.write("\r\ninsert".getBytes());
raf.close();