Java中io流的学习(九)RandomAccessFile

RandomAccessFile的实例支持对随机文件的读取和写入,这里的随机,值的是可以用RandomAccessFile对文件进行读和写操作,具体是读还是写,要根据设置的模式来决定。底层实际可以理解为一个byte数组,是带有指针的,通过指针的指向对文件进行读操作和写操作。

它的常用方法是:getFilePointer(),返回此文件中的当前偏移量,即返回指针位置;length(),返回此文件的长度;read(),read方法包含一系列参数和以read开头的方法,具体请看API,无非就是某些数据类型;readLine(),从此文件读取文本的下一行;seek(long pos),设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作,即设置指针位置,下次读写从该位置开始;skipBytes(int n),跳过输入的n个字节,丢弃跳过的字节;write(),write包含一系列参数和以write开头的方法,具体看API,无非就是某些数据类型。

下面我们通过实例代码来对其进行学习:

①利用随机访问流向文件写入数据

	@Test
	public void t1() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("H:\\javaio\\randomtest.txt", "rw");
		raf.write("ABC我是测试用例123".getBytes());
		raf.close();
	}

②利用随机访问流读取文件的数据

	@Test
	public void t2() throws Exception{
		RandomAccessFile raf =  new RandomAccessFile("H:\\javaio\\randomtest.txt", "r");
		byte[] b = new byte[1024];
		int len;
		while((len = raf.read(b)) != -1){
			System.out.println(new String(b, 0, len));
		}
		raf.close();
	}

③直接从某一位置开始读取数据,即跳过某一位置之前,使用seek

	@Test
	public void t3() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("H:\\javaio\\randomtest.txt", "r");
		//使用seek设置指针位置为3
		raf.seek(3);
		byte[] b = new byte[1024];
		int len;
		while((len = raf.read(b)) != -1){
			System.out.println(new String(b, 0, len));
		}
		raf.close();
	}

④如果不设置文件的指针,写入文件是从0下标开始,如果文件里面有数据,会覆盖掉从该下标开始到写入数据的结束位置,其它数据不变

	@Test
	public void t4() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("H:\\javaio\\randomtest.txt", "rw");
		raf.write("测试".getBytes());
		raf.close();
	}

⑤文件末尾追加信息

	@Test
	public void t5() throws Exception{
		RandomAccessFile raf = new RandomAccessFile("H:\\javaio\\randomtest.txt", "rw");
		raf.seek(raf.length());
		raf.write("追加信息".getBytes());
		raf.close();
	}

⑥大家在网上下载文件的时候,是不是有时候会遇到什么网络中断、电脑出问题的情况,本来下载好了80%,如果出了问题,有些就只有重新下载,而有些可以从失败处继续下载,不用再重新开始下载。它多的实现原理就是使用到了指针。我们用一个下载的文件实例来记录下载到的位置,然后通过读写操作,每次读写的位置就是从已经下载好的文件的末尾开始,下面通过代码来展示

	@Test
	public void t6() throws Exception{
		RandomAccessFile r = new RandomAccessFile("H:\\javaio\\测试.avi", "r");
        //这个file就是我们下载到本地的文件,之后用file.length来记录下载的大小,每次读取和写入就从file.length开始
		File file = new File("H:\\javaio\\下载.avi");
		RandomAccessFile w = new RandomAccessFile(file,"rw" );
		r.seek(file.length());
		w.seek(file.length());
		byte[] b = new byte[1024];
		int len;
		while((len = r.read(b)) != -1){
			w.write(b, 0, len);
		}
		w.close();
		r.close();
	}

Java中io流的学习(一)File:https://blog.csdn.net/qq_41061437/article/details/81672859

Java中io流的学习(二)FileInputStream和FileOutputStream:https://blog.csdn.net/qq_41061437/article/details/81742175

Java中io流的学习(三)BuffereInputStream和BuffereOutputStream:https://blog.csdn.net/qq_41061437/article/details/81743522

Java中io流的学习(四)InputStreamReader和OutputStreamWriter:https://blog.csdn.net/qq_41061437/article/details/81745300

Java中io流的学习(五)FileReader和FileWriter:https://blog.csdn.net/qq_41061437/article/details/81747105

Java中io流的学习(六)BufferedReader和BufferedWriter:https://blog.csdn.net/qq_41061437/article/details/81747323

Java中io流的学习(七)ObjectInputStream和ObjectOutputStream:https://blog.csdn.net/qq_41061437/article/details/81748461

Java中io流的学习(八)PrintStream和PrintWriter:https://blog.csdn.net/qq_41061437/article/details/81782770

Java中io流的学习(九)RandomAccessFile:https://blog.csdn.net/qq_41061437/article/details/81805351

Java中io流的学习(十)ByteArrayInoutStream和ByteArrayOutputStream:https://blog.csdn.net/qq_41061437/article/details/81806245

Java中io流的学习(十一)NIO:https://blog.csdn.net/qq_41061437/article/details/81809370

Java中io流的学习(总结):https://blog.csdn.net/qq_41061437/article/details/81740680

猜你喜欢

转载自blog.csdn.net/qq_41061437/article/details/81805351