JAVA高级基础(40)---RandomAccessFile

本文介绍了Java中的RandomAccessFile,它允许以读写模式访问文件。通过指定的指针,可以灵活定位读写位置。使用时需注意,readLine()方法默认使用ISO-8859-1编码解码字符串,建议查阅API获取详细方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RandomAccessFile

随机访问文件。可以通过在创建对象的时候,指定模式:r 读模式  /  rw  写模式

在其中存在指针,来定位对文件进行读写的位置

  • getPointFile(); 获取指针位置
  • seek(long  pos )设置指针位置

在读取文件的时候,我们要注意一问题:是否存在读取一个字符串的方式。在读的时候,要注意转换为字符串的时候的编码方式
readLine();但是该方法的默认的解码方式时ISO-8859-1

注:具体方法请查找API

相关代码

package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile  raf = new RandomAccessFile("aa.txt","r");
		//获取指针的位置
		System.out.println(raf.getFilePointer());// 0  开始指向文件的开始位置
		String strLine  =  raf.readLine();
		System.out.println(raf.getFilePointer());
		System.out.println(strLine);
		//因为读的时候使用的ISO-8859-1   需要重写进行解码和编码
		byte[] b = strLine.getBytes("ISO-8859-1");//使用ISO-8859-1 对读取到的内容进行解码
		String str = new String(b,"UTF-8");//再使用UTF-8重新进行编码
		System.out.println(str);
		//UTF-8 是修改版的UTF-8 会对读取到的内容添加两个字节		
		System.out.println(raf.getFilePointer());
	}
}
package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo1 {
	public static void main(String[] args) throws IOException {
		//创建randomaccessfile的读写模式
		RandomAccessFile  rafOut = new RandomAccessFile("bb.txt","rw");
		//读
		//RandomAccessFile rafIn  = new RandomAccessFile("bb.txt", "r");
		
		//准备写出的数据:
		int  i = 10;//4
		double d = 11.12;//8
		boolean b = true;//1
		String as = "太";// 3 + 2
		
		String s = "太原师范学院";//18 + 2
		//写
		rafOut.writeInt(i);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeDouble(d);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeBoolean(b);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeUTF(as);
		System.out.println("----" + rafOut.getFilePointer());
		rafOut.writeUTF(s);
		System.out.println("-----------" + rafOut.getFilePointer());
		//将指针移回到文件的开始位置
		rafOut.seek(18);
		String str = "山西省太原市";
		rafOut.writeUTF(str);
		rafOut.seek(18);
		
		//读
		/*int ii = rafOut.readInt();
		System.out.println(ii);
		double dd = rafOut.readDouble();
		System.out.println(dd);
		boolean bb = rafOut.readBoolean();
		System.out.println(bb);*/
		String ss = rafOut.readUTF();
		System.out.println(ss);
		
		
		//关闭
		rafOut.close();
		
		
	}
}
package org.lanqiao.randomaccessfile.demo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo3 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("cc.txt", "rw");
		byte b = 2;
		int i = 255;// 00000000 00000000  00000000 10000001
		raf.writeByte(b);//每次写一个字节
		raf.writeByte(i);
		raf.seek(0);
		System.out.println(raf.readByte());//每次读一个字节
		System.out.println(raf.readByte());
		byte b1 = (byte) (i >>> 24);
		byte b2 = (byte) (i >>> 16);
		byte b3 = (byte) (i >>> 8);
		byte b4 = (byte) i;
		System.out.println(b1);
		System.out.println(b2);
		System.out.println(b3);
		System.out.println(b4);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值