Java核心API之RandomAccessFile使用介绍

 时间:2024-10-12 16:22:18

1、RandomAccessFile的构造方法RandomAccessFi盟敢势袂le在文件随机访问操作时有两种模式,分别是只读模式和缥熹嵛郦读写模式两种。在这里提供两种RandomAccessFile类构造方法,RandomAccessFile(File file ,String mode)和RandomAccessFile(String filename, String mode),第一个参数是需要访问的文件,第二个参数是访问模式。只读模式代码如下:/* * 只读模式 * RandomAccessFile(File file,String mode) * RandomAccessFile(String filename,String mode) * RandomAccessFile构造方法第一个参数是要访问的文件, * 第二个参数是访问模式,"r"表示只读模式 */ @Test public void testReadMode() throws FileNotFoundException{ File file = new File("test.txt"); String filename = "test.txt"; RandomAccessFile raf = new RandomAccessFile(file,"r"); RandomAccessFile raf2 = new RandomAccessFile(filename,"r"); }注意:在只读模式下,若要访问的文件不存在则会抛出FileNotFoundException异常。

Java核心API之RandomAccessFile使用介绍

2、读写模式代码如下:/* * 读写模式 * RandomAccessFile(File file,String mode) * RandomAccessFile(String filename,String mode) * RandomAccessFile构造方法第一个参数是要访问的文件, * 第二个参数是访问模式,"rw"表示只读模式 */ @Test public void testReadWriteMode() throws FileNotFoundException{ File file = new File("test.txt"); String filename = "test.txt" RandomAccessFile raf = new RandomAccessFile(file,"rw"); RandomAccessFile raf2 = new RandomAccessFile(filename,"rw"); }注意:在读写模式下,若该文件不存在,写入数据时则会自动创建文件,若创建不成功,则抛出异常。

3、RandomAccessFile读方法代码如下: /* * write(int d)方法,该方法只写参数int的“低8位”, * 其实只写int型参数的一个字节 */ @Test public void testWrite() throws IOException{ RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); raf.write(1); }注意:write(int d)方法只能读取参数int的“低8位”,超过八位的字节无法读取,如写入256,其实只写入了0。

Java核心API之RandomAccessFile使用介绍

4、RandomAccessFile写方法代码如下: /* * read()方法,该方法读取字节的方法 ,该方法读取一个byte(8位)填充到int * 的低八位,高24位为0,返回值范围在0~255,如果返回-1表示读到了文 * 件末尾。 */ @Test public void testRead() throws IOException{ RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); int d = -1; while((d=raf.read())!=-1){ System.out.println(d); } }分析:read()方法读取一个字节(8位)填充到int的低八位,所以返回值高24位为0,返回范围在0~255之间,如果返回-1表示读到了文件末尾。

5、RandomAccessFile的read(byte[] b)方法示例代码如下:/* * read(byte[] b)可以从文件中批量读取字节的方法 * 该方法会从文件中尝试最多读取给定数组的总长度的字节量, * 并从给定的字节数组第一个位置开始,将读取到的字节顺序存放至数组中, * 返回值为实际读取到的字节量 */@Test public void testReadByByteArray() throws IOException{ RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); RandomAccessFile raf2 = new RandomAccessFile("test_copy.txt","rw"); byte[] b = new byte[1024*10]; int len = -1; while((len=raf2.read(b))!=-1){ raf.write(b,0,len); } }分析:read(byte[] b)批量读取字节,参数是字节数组,即批量读取字节的数据,通俗讲就是该方法读取这么多字节数组所能装下的数据。字节数组的长度一般创建为1024*10,即为10K,这样JVM能够以最佳性能读取文件内容。

Java核心API之RandomAccessFile使用介绍

6、RandomAccessFile的write(byte[] d,int offset,int len)方法RandomAccessFile还有许多写入方法,如wr足毂忍珩iteInt(),writeLong()...writeXXX()方法,这次主要讲出场率高的write(byte[] d,int offset,int len)方法。示例代码如下:/* * write(byte[] d,int offset,int len) * 该方法会根据当前指针所在位置处连续写出改定数组中的部分字节, * 这个部分是从数组的offset处开始,连续读len个字节 */ @Test public void testWriteByByteArray() throws IOException{ RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); RandomAccessFile raf2 = new RandomAccessFile("test_copy.txt","rw"); byte[] b = new byte[1024*10]; int len = -1; while((len=raf.read(b))!=-1){ raf2.write(b,0,len); } }分析:该方法通俗讲,写入字节数组所装的数据,但是就上面的例子而言,如果读取的数据达到10K了,自然全部写入,如果读的数据不足10K,就4K,那么就要确定一下写入的数据长度了,于是write(byte[] b)有了一个重载方法write(byte[] b,int offset ,int len)

7、RandomAccessFile的文件指针操作的方法RandomAccessFile是基于指针操作文件的读写的,操作指针的位置,就可以任意的读写文件了。代码如下: /* * long getFilePointer() * 该方法获取当前RandomAccessFile的指针位置 */ @Test public void testGetFilePointer() throws IOException { RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); System.out.println(raf.getFilePointer()); //0 raf.writeInt(1); System.out.println(raf.getFilePointer()); //4 }这个方法比较简单,获取指针位置。

Java核心API之RandomAccessFile使用介绍

8、RandomAccessFile的seek()方法代码如下: /* * void seek(long pos) * 该方法用于移动指针位置 */ @Test public void testSeek() throws IOException{ RandomAccessFile raf = new RandomAccessFile("test.txt","rw"); System.out.println(raf.getFilePointer());//0 raf.writeInt(1); System.out.println(raf.getFilePointer());//4 raf.seek(0); System.out.println(raf.getFilePointer());//0 }该方法可以移动指针到任意位置,读取数据就是这么任性,不服不行。

Java核心API之RandomAccessFile使用介绍

9、RandomAccessFile的close()方法RandomAccessFile文件访问结束后,别忘记用close()方法释放资源,作为一名环保卫士,节约能源人人有责。

  • Python教程 输出Apple的每个字母
  • 俄罗斯方块如何用html5编辑
  • 向量的定理的基本应用?
  • 中小学老师PPT模板快速制作
  • 广播域和冲突域怎么数
  • 热门搜索
    三爱三节手抄报 关于安全的手抄报 节约用水手抄报 母亲节手抄报 家风家训手抄报 科学手抄报 春节手抄报内容 中秋节的手抄报 防震减灾手抄报内容 国庆节手抄报内容