zoukankan      html  css  js  c++  java
  • 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章。

  • 相关阅读:
    使用阿里云docker加速器
    Linux之screen命令详解
    Linux下Git和GitHub使用方法总结
    CentOS 6&7安装ffmpeg
    用yum安装lamp和lnmp环境
    nginx错误日志error_log日志级别
    CentOS7 yum 安装mysql 5.6
    python实现对数据的写入和读取(excel)
    windows下配置sublime
    远程配置pycharm
  • 原文地址:https://www.cnblogs.com/Nojava/p/13453538.html
Copyright © 2011-2022 走看看