RandomAccessFile

seek(),指定位置读取

package IOStudy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

/**
 * 随机流
 * 随机读取和写入流
 * RandomAccessFile
 * @author pmc
 *
 */
public class RandoAccessFileTest {
	public static void main(String[] args) throws IOException {
//		RaFile();
		System.out.println("***");
		RaFile02();
		System.out.println("***");
		RaFile03();
		
	}
	/**
	 * 指定起始位置读取剩余所有内容
	 * @throws IOException
	 */
	private static void RaFile() throws IOException{
		RandomAccessFile raf=new RandomAccessFile(new File("txts.txt"),"r");
		//随机读取
		/**
		 * seek(2)读取第二个字节以后的内容
		 */
		raf.seek(2);
		//分段读
		byte[] temp=new byte[1024];
		int len=0;
		while((len=raf.read(temp))!=-1){
			System.out.print(new String (temp,0,len));
		}
		raf.close();
	}
	//起始、实际大小
	private static void RaFile02() throws IOException{
		RandomAccessFile raf=new RandomAccessFile(new File("txts.txt"),"r");
		//起始位置
		int beginPos=2;
		//实际大小
		int actualSize=20;
		//随机读取
		/**
		 * seek(2)读取第二个字节以后的内容
		 */
		raf.seek(beginPos);
		//分段读
		byte[] temp=new byte[12];
		int len=0;
		while((len=raf.read(temp))!=-1){
			if(actualSize>len){//如果大所有内容都要
				System.out.println("第一次读");
				System.out.print(new String (temp,0,len)+"\n");	
				actualSize-=len;//剩余需要读取的数量
			}else{
				System.out.println("第二次读");
				System.out.print(new String (temp,0,actualSize)+"\n");
				break;
			}
		}
		raf.close();
	}
	//分块
	private static void RaFile03() throws IOException{
		//创建源
		File src=new File("txts.txt");
		//总长度
		long len=src.length();
		System.out.println("文件大小:"+len);
		//每块大小
		long blockSize=50;
		//多少块
		long size=(long)Math.ceil((double)len/blockSize);//向上取整
		System.out.println(size+"块");
		//起始位置
		long qs=0;
		//实际大小,如果每块大小大于总长度则取总长度,否则取指定长度
		long sj=blockSize>len?len:blockSize;
		for(int i=0;i<size;i++){
			qs=i*blockSize;	
			
			if(i==(size-1)){
				blockSize=len;
			}else{
				sj=blockSize;//每次读10个
				len-=sj;//剩余长度
			}
			System.out.println("第"+i+"块 "+"起始位置:"+qs+"实际大小:"+blockSize);
			RaFile04(i,qs,sj);
		}
	}
	/**
	 * 第几块
	 * 起始位置
	 * 实际大小
	 * @param i
	 * @param qs
	 * @param sj
	 * @throws IOException
	 */
		private static void RaFile04(long i,long qs,long sj) throws IOException{
			RandomAccessFile raf=new RandomAccessFile(new File("txts.txt"),"r");
			//起始位置
//			int beginPos=2;
			//实际大小
//			int actualSize=20;
			//随机读取
			/**
			 * seek(2)读取第二个字节以后的内容
			 */
			raf.seek(qs);
			//分段读
			byte[] temp=new byte[12];
			int len=0;
			while((len=raf.read(temp))!=-1){
				if(sj>len){//如果大所有内容都要
//					System.out.println("第一次读");
					System.out.print(new String (temp,0,len));	
					sj-=len;//剩余需要读取的数量
				}else{
//					System.out.println("第二次读");
					System.out.print(new String (temp,0,(int)sj));
					break;
				}
			}
			raf.close();
		}
}

封装分割方法

package IOStudy;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * 随机流
 * 随机读取和写入流
 * RandomAccessFile
 * seek() 指定从何开始读取
 * 面向过程
 * 面向对象封装
 * @author pmc
 *
 */
public class RandoAccessFileTest2 {
	//源头
	private File src;
	//目的地(文件夹)
	private String dir;
	//所有分割后的文件存储路径
	private List<String> Path;
	//每块大小
	private int blockSize;
	//块数:多少块
	private int size;
	public RandoAccessFileTest2(String src,String dir,int blockSize){
		this.src=new File(src);
		this.dir=dir;
		this.blockSize=blockSize;
		this.Path=new ArrayList<String>();
		//初始化
		init();
	}
	//初始化
	private void init(){
		//总长度
		long len=this.src.length();
		//块数:多少块
		this.size=(int)Math.ceil(len*1.0/this.blockSize);
		//路径
		for(int i=0;i<size;i++){
			this.Path.add(this.dir+"/"+i+"-"+this.src.getName());
		}
	}
	/**
	 * 分割
	 * @throws IOException 
	 */
	public void split() throws IOException{
		// 创建源
//		File src = new File("abc.png");
		// 总长度
		long len = this.src.length();
//		System.out.println("文件大小:" + len);
		// 每块大小
//		long blockSize = 50;
		// 多少块
//		long size = (long) Math.ceil((double) len / blockSize);// 向上取整
//		System.out.println(size + "块");
		// 起始位置
		long qs = 0;
		// 实际大小,如果每块大小大于总长度则取总长度,否则取指定长度
		long sj = blockSize > len ? len : blockSize;
		for (int i = 0; i < size; i++) {
			qs = i * blockSize;

			if (i == (size - 1)) {
				blockSize = (int) len;
			} else {
				sj = blockSize;// 每次读10个
				len -= sj;// 剩余长度
			}
			System.out.println("第" + i + "块 " + "起始位置:" + qs + "实际大小:" + blockSize);
			RaFile05(i, qs, sj);
		}
	} 
	
	/**
	 * 分割输出
	 * @param args
	 * @throws IOException
	 */
	private  void RaFile05(long i,long qs,long sj) throws IOException{
		RandomAccessFile raf=new RandomAccessFile(this.src,"r");
		RandomAccessFile rafw=new RandomAccessFile(this.Path.get((int)i),"rw");
		//起始位置
//		int beginPos=2;
		//实际大小
//		int actualSize=20;
		//随机读取
		/**
		 * seek(2)读取第二个字节以后的内容
		 */
		raf.seek(qs);
		//分段读
		byte[] temp=new byte[12];
		int len=0;
		while((len=raf.read(temp))!=-1){
			if(sj>len){//如果大所有内容都要
//				System.out.println("第一次读");
//				System.out.print(new String (temp,0,len));	
				rafw.write(temp,0,len);
				sj-=len;//剩余需要读取的数量
			}else{
//				System.out.println("第二次读");
//				System.out.print(new String (temp,0,(int)sj));
				rafw.write(temp,0,(int)sj);
				break;
			}
		}
		rafw.close();
		raf.close();
	}
	public static void main(String[] args) throws IOException {
//		RaFile();
		System.out.println("***");
//		RaFile02();
		System.out.println("***");
//		RaFile03();
		System.out.println("***");
		RandoAccessFileTest2 ft=new RandoAccessFileTest2("src.txt","temp",50);
		ft.split();
	}
	/**
	 * 指定起始位置读取剩余所有内容
	 * @throws IOException
	 */
	private static void RaFile() throws IOException{
		RandomAccessFile raf=new RandomAccessFile(new File("txts.txt"),"r");
		//随机读取
		/**
		 * seek(2)读取第二个字节以后的内容
		 */
		raf.seek(2);
		//分段读
		byte[] temp=new byte[1024];
		int len=0;
		while((len=raf.read(temp))!=-1){
			System.out.print(new String (temp,0,len));
		}
		raf.close();
	}
	//起始、实际大小
	private static void RaFile02() throws IOException{
		RandomAccessFile raf=new RandomAccessFile(new File("txts.txt"),"r");
		//起始位置
		int beginPos=2;
		//实际大小
		int actualSize=20;
		//随机读取
		/**
		 * seek(2)读取第二个字节以后的内容
		 */
		raf.seek(beginPos);
		//分段读
		byte[] temp=new byte[12];
		int len=0;
		while((len=raf.read(temp))!=-1){
			if(actualSize>len){//如果大所有内容都要
				System.out.println("第一次读");
				System.out.print(new String (temp,0,len)+"\n");	
				actualSize-=len;//剩余需要读取的数量
			}else{
				System.out.println("第二次读");
				System.out.print(new String (temp,0,actualSize)+"\n");
				break;
			}
		}
		raf.close();
	}
	//分块
	private static void RaFile03() throws IOException{
		//创建源
		File src=new File("abc.png");
		//总长度
		long len=src.length();
		System.out.println("文件大小:"+len);
		//每块大小
		long blockSize=50;
		//多少块
		long size=(long)Math.ceil((double)len/blockSize);//向上取整
		System.out.println(size+"块");
		//起始位置
		long qs=0;
		//实际大小,如果每块大小大于总长度则取总长度,否则取指定长度
		long sj=blockSize>len?len:blockSize;
		for(int i=0;i<size;i++){
			qs=i*blockSize;	
			
			if(i==(size-1)){
				blockSize=len;
			}else{
				sj=blockSize;//每次读10个
				len-=sj;//剩余长度
			}
			System.out.println("第"+i+"块 "+"起始位置:"+qs+"实际大小:"+blockSize);
			RaFile04(i,qs,sj);
		}
	}
	/**
	 * 第几块
	 * 起始位置
	 * 实际大小
	 * @param i
	 * @param qs
	 * @param sj
	 * @throws IOException
	 */
		private static void RaFile04(long i,long qs,long sj) throws IOException{
			RandomAccessFile raf=new RandomAccessFile(new File("abc.png"),"r");
			RandomAccessFile rafw=new RandomAccessFile(new File("temp/"+i+"abc.png"),"rw");
			
			//起始位置
//			int beginPos=2;
			//实际大小
//			int actualSize=20;
			//随机读取
			/**
			 * seek(2)读取第二个字节以后的内容
			 */
			raf.seek(qs);
			//分段读
			byte[] temp=new byte[12];
			int len=0;
			while((len=raf.read(temp))!=-1){
				if(sj>len){//如果大所有内容都要
//					System.out.println("第一次读");
//					System.out.print(new String (temp,0,len));	
					rafw.write(temp,0,len);
					sj-=len;//剩余需要读取的数量
				}else{
//					System.out.println("第二次读");
//					System.out.print(new String (temp,0,(int)sj));
					rafw.write(temp,0,(int)sj);
					break;
				}
			}
			rafw.close();
			raf.close();
		}
}

 

Logo

华为AI开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐