java-装饰流-随机流

  • Post author:
  • Post category:java


RandomAccessFile 随机流:支持读取和写入

seek方法随机访问,可以对文件进行分割



1.指定起始位置,读取剩余的所有内容

package cn.lesson.Burrfed;

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

/**
 * 随机读取和写入流RandomAccessFile
 * @author MacBook
 *
 */
public class RandonTest01 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		RandomAccessFile raf = new RandomAccessFile(new File("D:\\e- java\\DealWithIO\\src\\cn\\lesson\\Burrfed\\copy.java"),"r");
		//随机读取
		raf.seek(2);
		//读取
		byte[] flush = new byte[1024];//缓冲容器
		int len=-1;
		while((len=raf.read(flush))!=-1) {
			System.out.println(new String(flush,0,len));
		}
		raf.close();
	}

}



2.分块思想:起始、实际大小

package cn.lesson.Burrfed;

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

/**
 * 随机读取和写入流RandomAccessFile
 * @author MacBook
 *
 */
public class RandonTest01 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		RandomAccessFile raf = new RandomAccessFile(new File("D:\\e- java\\DealWithIO\\src\\cn\\lesson\\Burrfed\\copy.java"),"r");
		//起始位置
		int beginPos = 2;
		//实际大小
		int actualSize =1026;
		
		
		
		//随机读取
		raf.seek(beginPos);
		//读取
		byte[] flush = new byte[1024];//缓冲容器
		int len=-1;
		while((len=raf.read(flush))!=-1) {
			
			
			if(actualSize>len) {
				System.out.println(new String(flush,0,len));
				actualSize-=len;
			}else {
				System.out.println(new String(flush,0,actualSize));
				break;
			}
		}
		raf.close();
	}

}




3.起始位置、实际大小、块数

package cn.lesson.Burrfed;

import java.io.File;

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

/**
 * 随机读取和写入流RandomAccessFile
 * @author MacBook
 *
 */
public class RandonTest01 {

	public static void main(String[] args) throws IOException {
		//分多少块
		File src = new File("D:/e- java/DealWithIO/src/cn/lesson/Burrfed/ConverTest01.java");
		//总长度
		long len =src.length();
		//每一块的大小
		int blockSize = 101;
		//需要多少块
		int size = (int)Math.ceil(len*1.0/blockSize);
//		System.out.println(size);
		
		//起始位置
		int beginpos=0;
		//实际大小
		int actualSize = (int)(blockSize>len?len:blockSize);
		for(int i =0;i<size;i++) {
			beginpos=i*blockSize;
			if(i==size-1) {//最后一块
				actualSize=(int)len;
			}else {//其他的
				actualSize=blockSize;
				len-=actualSize;//剩余量
			}
			System.out.println(i+"->"+beginpos+"->"+actualSize);
			split(i,beginpos,actualSize);
		}
		
		
	}




/**
 * 第i指定起始位置和实际长度
 * @param i
 * @param beginPos
 * @param actualSize
 * @throws IOException
 */
	public static void split(int i,int beginPos,int actualSize ) throws IOException {
		// TODO Auto-generated method stub
		RandomAccessFile raf = new RandomAccessFile(new File("D:\\e- java\\DealWithIO\\src\\cn\\lesson\\Burrfed\\copy.java"),"r");
		//起始位置
	
		//实际大小
	
		//随机读取
		raf.seek(beginPos);
		//读取
		byte[] flush = new byte[1024];//缓冲容器
		int len=-1;
		while((len=raf.read(flush))!=-1) {
			
			
			if(actualSize>len) {
				System.out.println(new String(flush,0,len));
				actualSize-=len;
			}else {
				System.out.println(new String(flush,0,actualSize));
				break;
			}
		}
		raf.close();
	}
}



版权声明:本文为qq_43287650原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。