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章。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步