本文共 1149 字,大约阅读时间需要 3 分钟。
package RandTest_11;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class RandTest_3 { public static void main(String[] args) throws IOException { File src = new File("a.html"); long len = src.length(); //总长度 int blockSize = 1024; //块大小 int size = (int) Math.ceil((len*1.0/blockSize)); //块个数 System.out.println(size); int beginPos = 0; //起始位置 int actualSize; //实际大小 for(int i = 0 ; i"+beginPos+"-->"+actualSize); split(beginPos, actualSize); } } public static void split(int beginPos,int actualSize) throws IOException { RandomAccessFile raf = new RandomAccessFile(new File("a.html"), "r");// System.out.println(raf.length()); raf.seek(beginPos); //设置起始位置 byte[] datas = new byte[1024]; //缓冲容器 int len = -1; while((len=raf.read(datas))!=-1) { if(actualSize>len) { //获取本次读取的所有内容 System.out.println(new String(datas,0,len,"UTF-8")); actualSize -=len;// System.out.println("测试"); }else { System.out.println(new String(datas,0,actualSize,"UTF-8")); break; } } raf.close(); }}
转载地址:http://fisnb.baihongyu.com/