2020.8.7第三十二天

1.今天学习RandomAccessFile随机访问文件

RandomAccessFile可以随机读写文件,随机读写文件就是说可以任意访问文件的位置,
这是其他流所不能操纵的。RandomAccessFile类包含一个记录指针,用于标识当前流的读
写位置,这个位置可以向前移动,也可以向后移动。RandomAccessFile包含两个方法来操
作文件记录指针。
long getFilePoint():记录文件指针的当前位置。
void seek(long pos):将文件记录指针定位到pos位置。

public RandomAccessPile(Eile file,String mode) throws FileNotFoundException
public RandomAccessFile(String name,String mode) throws FileNotFoundException

 

 

复制代码
 1 import java.io.IOException;
 2 import java.io.RandomAccessFile;
 3 public class RandomAccessFileDemo {
 4 public static void main (String[] args)throws IOException {
 5 RandomAccessFile acf=
 6 new RandomAccessFile("D:/Hello.txt", "rw");
 7 byte[] b = "Hello Java!!".getBytes();
 8 acf.write(b);
 9 acf.seek(6);
10 System.out.println ("pointer="+acf.getFilePointer());
11 byte[] b2="C++".getBytes();
12 acf.write(b2);
13 int len=-1;
14 byte[] buf=new byte[1024];
15 acf.seek(0);
16 System.out.println ("pointer="+acf.getFilePointer());
17 while((len=acf.read (buf))!=-1) {
18 String s =new String(buf,0,len);
19 System.out.println(s);
20 }
21 }
22 }
复制代码

 

 2.遇到的问题:不知道如何定位指针

3.明天复习第12章。

posted @   敲敲代代码码  阅读(86)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示