Java IO : RandomAccessFile

来源:互联网 发布:唐软网络是培训吗 编辑:程序博客网 时间:2024/05/21 06:18

RandomAccessFile 类

【java】


RandomAccessFile 支持“随机访问”的方式,程序可以直接跳转到文件的任意地方来读写数据。即既支持读取文件内容,也支持向文件输出数据。

RandomAccessFile 对象包含了一个记录指针,用以标识当前读写处的位置,新创建一个RandomAccessFile对象时,该对象的文件记录指针位于0处,当读/写了n个字节后,文件记录指针将会向后移动n个字节。

操作文件记录指针的方法:

  • long getFilePointer() : 返回文件记录指针的当前位置。
  • void seek(long pos) : 将文件记录指针定位到pos位置。

构造方法如下:

public RandomAccessFile(String name, String mode) throws FileNotFoundException {       this(name != null ? new File(name) : null, mode);}public RandomAccessFile(File file, String mode) {    ......}

mode参数有以下值:

  • “r”: 只读,如果写报IOException。
  • “rw” : 读写,不存在就创建新文件
  • “rws”
  • “rwd”

访问指定内容

    public static void main(String[] args) throws IOException{        RandomAccessFile raf =null;         try {        //以只读方式打开一个RandomAccessFile对象            raf = new RandomAccessFile("D:/ZhyTestSpace/testBase/src/test/java/testBase/RandomAccessFileTest.java", "r");            System.out.println("RandomAccessFile的文件指针的初始位置:" + raf.getFilePointer());            //移动文件指针的位置            raf.seek(300);            byte[] bbuf=new byte[1024];             int hasRead = 0;             while ((hasRead = raf.read(bbuf)) > 0) {                System.out.println(new String(bbuf,0,hasRead));            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }         finally {            if (raf !=null){                raf.close();            }        }    }}

向指定文件追加内容

public class RandomAccessFileTest {    public static void main(String[] args) throws IOException{        RandomAccessFile raf= null;        try {            raf = new RandomAccessFile("E:/javaFile/out.txt", "rw");            raf.seek(raf.length());            raf.write("追加的内容!\r\n".getBytes());        } catch (FileNotFoundException e) {            e.printStackTrace();        }        finally {            if(raf !=null){                raf.close();            }        }    }}

运行上面的程序,可以看到out.txt文件中多一行“追加的内容!”字符串。

向指定文件,指定位置插入内容

public class RandomAccessFileTest {    public static void main(String[] args) throws IOException {        insert("D:/ZhyTestSpace/testBase/src/test/java/testBase/RandomAccessFileTest.java", 400, "\r\n//这是新插入的文本内容哦!!!\r\n");    }    public static void insert(String fileName, long pos, String insertContent) throws IOException {        RandomAccessFile raf = null;        File file = new File("E:/javaFile");        File tmp = File.createTempFile("tmp", null,file);        FileOutputStream tmpOut = null;        FileInputStream tmpIn = null;        tmp.deleteOnExit();        try {            raf = new RandomAccessFile(fileName, "rw");            tmpOut = new FileOutputStream(tmp);            tmpIn = new FileInputStream(tmp);            raf.seek(pos);            // -----下面代码将插入点后的内容读入临时文件中保存-----            byte[] bbuf = new byte[64];            int hasRead = 0;            while ((hasRead = raf.read(bbuf)) > 0) {                // 将读取的数据写入临时文件                tmpOut.write(bbuf, 0, hasRead);            }            // -----------下面代码插入内容------------            // 把文件记录指针重新定位到pos位置            raf.seek(pos);            // 追加需要插入的内容            raf.write(insertContent.getBytes());            // 追加临时文件中的内容            while ((hasRead = tmpIn.read(bbuf)) > 0) {                raf.write(bbuf, 0, hasRead);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (raf != null) {                raf.close();            }        }    }}

运行上面的程序,RandomAccessFile.java 文件将插入了一行字符串,上面程序,先使用了createTempFile() 方法创建了一个临时文件,用来保存被插入文件的插入点后面的内容。程序先将文件中插入点后的内容读入到临时文件中,接下来重新定位到插入点,将需要插入的内容添加到文件后面,最后再将临时文件的内容添加到文件后面,这样就达到了向指定文件、指定位置插入内容的效果。

0 0
原创粉丝点击