【JAVA面试题】之二:Java RandomAccessFile用法

 《JAVA面试宝典》中6.3提到了RandomAccessFile,顺便看了下,摘抄如下:

文章:
http://blog.csdn.net/akon_vm/article/details/7429245
说明RandomAccessFile的绝大多数功能,但不是全部,已经被JDK 1.4 nio的
“内存映射文件(memory-mapped files)”给取代了。
RandomAccessFile可以用在断点续传功能上。

文章:http://qindongliang.iteye.com/blog/1972146则用源码解释了用法。

附上源码:
package com.baidian.iostream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

public class TestRandomAccessFile {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String path = "E:\\Android\\test.txt";
        int seekPointer = 20;  
        randomWrite(path);//追加写的方法  
        insert(path, 33, "\nlucene是一个优秀的全文检索库");
        randomRed(path,seekPointer);//读取的方法   
    }

    /**
     * 读的方法
     * 
     * @param path
     *            文件路径
     * @param pointe
     *            指针位置
     * **/
    public static void randomRed(String path, int pointe) {
        try {
            // RandomAccessFile raf=new RandomAccessFile(new
            // File("D:\\3\\test.txt"), "r");
            /**
             * model各个参数详解 
             * r 代表以只读方式打开指定文件
             * rw 以读写方式打开指定文件 
             * rws 读写方式打开,并对内容或元数据都同步写入底层存储设备
             * rwd 读写方式打开,对文件内容的更新同步更新至底层存储设备
             * **/
            RandomAccessFile raf = new RandomAccessFile(path, "r");
            // 获取RandomAccessFile对象文件指针的位置,初始位置是0
            System.out.println("RandomAccessFile文件指针的初始位置:"
                    + raf.getFilePointer());
            raf.seek(pointe);// 移动文件指针位置
            byte[] buff = new byte[1024];
            // 用于保存实际读取的字节数
            int hasRead = 0;
            // 循环读取
            while ((hasRead = raf.read(buff)) > 0) {
                // 打印读取的内容,并将字节转为字符串输入
                System.out.println(new String(buff, 0, hasRead));
            }
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 追加方式 写的方法
     * 
     * @param path
     *            文件路径
     * ***/
    public static void randomWrite(String path) {
        try {
            /** 以读写的方式建立一个RandomAccessFile对象 **/
            RandomAccessFile raf = new RandomAccessFile(path, "rw");

            // 将记录指针移动到文件最后
            raf.seek(raf.length());
            raf.write("我是追加的 \r\n".getBytes());
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 实现向指定位置 插入数据
     * 
     * @param fileName
     *            文件名
     * @param points
     *            指针位置
     * @param insertContent
     *            插入内容
     * **/
    public static void insert(String fileName, long points, String insertContent) {
        try {
            File tmp = File.createTempFile("tmp", null);
            tmp.deleteOnExit();// 在JVM退出时删除

            RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
            // 创建一个临时文件夹来保存插入点后的数据
            FileOutputStream tmpOut = new FileOutputStream(tmp);
            FileInputStream tmpIn = new FileInputStream(tmp);
            raf.seek(points);
            /** 将插入点后的内容读入临时文件夹 **/

            byte[] buff = new byte[1024];
            // 用于保存临时读取的字节数
            int hasRead = 0;
            // 循环读取插入点后的内容
            while ((hasRead = raf.read(buff)) > 0) {
                // 将读取的数据写入临时文件中
                tmpOut.write(buff, 0, hasRead);
            }

            // 插入需要指定添加的数据
            raf.seek(points);// 返回原来的插入处
            // 追加需要追加的内容
            raf.write(insertContent.getBytes());
            // 最后追加临时文件中的内容
            while ((hasRead = tmpIn.read(buff)) > 0) {
                raf.write(buff, 0, hasRead);
            }
            raf.close();
            tmpOut.close();
            tmpIn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值