RandomAccessFile,read,write,复制文件,指针,批量读写,基本类型读写

//(1)read------------------
public class RandomAccessFileRead {

    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("demo.dat","r");
        /**
         * 从当前文件中读取一个字节
         * 将该字节存入返回的int值中
         * int值“低8位”有效(前24位都是0)
         * 
         * 若返回值为-1,说明读取到文件的末尾
         * EOF end of file
         */
        int i = raf.read();
        System.out.println(i);

        i = raf.read();
        System.out.println(i);

        i = raf.read();
        System.out.println(i);
        raf.close();

    }

}
//(2)write-----------------------------------
/**
 * RandomAccessfile:用于读写文件数据的类
 * 是在当前指定的位置读写字节
 */
public class RandomAccessFileWrite {
    public static void main(String[] args) throws IOException {
        /**
         * 对项目根目录下的demo.dat文件进行读写
         */
        RandomAccessFile raf = new RandomAccessFile("demo.dat","rw");//读写"rw"或只读"r",没有只写

        /*
         * 以下方式也可

            File file = new File("demo.dat");
            RandomAccessFile raf2 = new RandomAccessFile(file,"rw");
         */

        /**
         * void write(int n)
         * 写出给定int值的“低8位”,即最后8位二进制数
         * 只写一个字节
         */
        //int num = 256;//100000000
        int num =255;//11111111
        System.out.println(Integer.toBinaryString(num));
        raf.write(num);//从最开始写,覆盖之前的内容
        raf.write(1);
        num =-1;
        System.out.println(Integer.toBinaryString(num));//11111111111111111111111111111111
        raf.write(num);//11111111
        raf.close();//使用IO后,一定要记得关闭
    }

}
//(3)RandomAccessFile 文件复制-----------------------
public class RandomAccessFileCopy {

    public static void main(String[] args) throws IOException {
        RandomAccessFile src = new RandomAccessFile("src.txt","r");
        RandomAccessFile des = new RandomAccessFile("copy.txt","rw");

        int d = -1;
        while((d=src.read())!=-1){
            des.write(d);
        }
        src.close();
        des.close();
    }

}
//(4)获得指针getFilePoint,移动指针seek----------
public class RandomAccessFileDemo {

    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
        /**
         * long getFilePoint()
         * 获得当前RAF的指针位置
         */
        long p = raf.getFilePointer();
        System.out.println(p);//0

        raf.write(97);//写一个字节
        System.out.println(raf.getFilePointer());//1

        raf.writeInt(1);//连续写4个字节
        System.out.println(raf.getFilePointer());//5

        System.out.println(raf.read());//-1,读取一个字节,文件的末尾
        /**
         * 若想从某个地方读取字节,需要移动指针
         * void seek(long position),将指针移动到某个位置
         */
        raf.seek(0);
        System.out.println(raf.read());//97

    }

}
//(5)指针往后跳skipBytes-----------------
public class RandomAccessFileDemo2 {
    /**
     * int skipBytes(int n)
     * 该方法会跳过n个字节,
     * 返回值为实际跳过的字节数
     * 只能往后跳,不能往前跳
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
        int i = raf.skipBytes(1);
        System.out.println(i);
        System.out.println(raf.read());

    }

}
/**
 * //(6)--------------
 * 读写数据时,若想提高读写效率,
 * 就必须减少读写次数
 */
public class RandomAccessFileDemo4 {
/**
 * 批量写出一组字节
 * @throws IOException 
 */
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("t.txt","rw");
        String str = "我";//内存里是unicode编码,2字节
        /**
         * String的getBytes()方法:
         * 将当前的字符串按照系统默认的字符集转为对应的字节
         * 
         * UTF-8:中文占3个字节,英文一个字节
         * GBK:中文2个字节,英文一个
         */
        //内存里是内存里是unicode编码,2字节,java去UTF-8编码里去找“我”的编码
        byte[] buf = str.getBytes("UTF-8");
        raf.write(buf);//结果:t.txt为3个字节
        raf.close();

    }

}
//(7)批量读取数据---------
public class RandomAccessFileDemo57 {

    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("t.txt","r");
        /**
         * int read(byte[] buf)
         * 一次从文件中读取buf数组length个字节
         * 并从该数组的第一个位置处起存放实际读取到的字节
         * 返回值为实际读取到的字节数
         */
        byte[] buf = new byte[50];
        int length = raf.read(buf);
        System.out.println(length);//3,因为"我"在utf-8编码里占3个字节
        System.out.println(Arrays.toString(buf));
        //[-26, -120, -111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]



    }

}

//(8)每次读写10K复制文件--------
public class RandomAccessFileDemo68 {

    public static void main(String[] args) throws IOException {
        RandomAccessFile src = new  RandomAccessFile("src.jpg","r");
        RandomAccessFile des = new RandomAccessFile("copy.jpg","rw");
        byte[] buf = new byte[10240];//10K
        int len = -1;
        while((len=src.read(buf))!=-1){
            des.write(buf);
        }
        src.close();
        des.close();
    }

}
//(9)向文件中写入基本类型数据-----------------
public class RandomAccessFileDemo79 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
        int num = Integer.MAX_VALUE;
        //将int写入文件
        /**
         * 01111111 11111111 11111111 11111111
         * num>>>24:右移24位,前面补0
         * 
         * write()写的是低8位
         */
        raf.write(num>>>24);
        raf.write(num>>>16);
        raf.write(num>>>8);
        raf.write(num);

        raf.writeInt(num);//这句等价于上面4句

        raf.writeDouble(1.42);
        raf.writeLong(123);

        int i = raf.readInt();
        System.out.println(i);
        raf.close();

    }

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值