最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

隨機(jī)流分塊代碼

2020-04-05 11:33 作者:小垃圾kiki  | 我要投稿
package cn.jd.io;

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

/*
?* 隨機(jī)讀取和寫(xiě)入流RandomAccessFile
?*
?*/
public class RandTest01 {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//分多少塊?? 首先給一個(gè)需要分塊的文件
?? ??? ?File src=new File("src/cn/jd/io/Copy.java");
?? ??? ?//文件的總長(zhǎng)度
?? ??? ?long? len=src.length();
//?? ??? ?System.out.println(len);
?? ??? ?//每塊大小
?? ??? ?int? blockSize=1024;
?? ??? ?//分多少塊
?? ??? ?int size=(int)Math.ceil(len*1.0/blockSize);//向上取整的意思就是3.5變成4
//?? ??? ?double a=6.0;
//?? ??? ?int b=4;
//?? ??? ?System.out.println(a/b);
?? ??? ?//思路每一塊從哪里開(kāi)始到哪里結(jié)束
?? ??? ?int beginPos=0;
?? ??? ?int actualSize=(int)(blockSize>len?len:blockSize);? //如果blockSize大于len,那么就去len的長(zhǎng)度
?? ??? ?for(int i=0;i<size;i++) {
?? ??? ??? ?beginPos=i*blockSize;
?? ??? ??? ?if(i==size-1) {//最后一塊
?? ??? ??? ??? ?actualSize=(int)len;
?? ??? ??? ?}else {
?? ??? ??? ??? ?actualSize=blockSize;
?? ??? ??? ??? ?len-=actualSize;//剩余長(zhǎng)度
?? ??? ??? ?}
?? ??? ??? ?System.out.println(i+"-->"+beginPos+"-->"+actualSize);
?? ??? ??? ?test2(i,beginPos,actualSize);
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?
?? ?}
?? ?
?? ?//分塊思想:有了起始索引,有了實(shí)際大小
?? ?//指定第i塊的起始位置和實(shí)際長(zhǎng)度
?? ?public static void test2(int i,int beginPos,int actualSize) throws IOException {
?? ??? ?RandomAccessFile? raf=new RandomAccessFile(new File("src/cn/jd/io/Copy.java"), "r");
//?? ??? ?//起始位置
//?? ??? ?int beginPos=2;
//?? ??? ?//實(shí)際大小
//?? ??? ?int actualSize=1026;//我要讀取1026個(gè)字節(jié)
//?? ??? ?//隨機(jī)讀取
//?? ??? ?raf.seek(beginPos); //從第二個(gè)索引開(kāi)始讀取,記住索引是從0開(kāi)始的
?? ??? ?//讀取??? 讀取方式和字節(jié)流的讀取方式是一樣的
?? ??? ?byte[]? flush=new byte[1024];
?? ??? ?int len=-1;
?? ??? ?while((len=raf.read(flush))!=-1) {
?? ??? ??? ?
?? ??? ??? ?if(len<=actualSize) {//獲取本次讀取的所有內(nèi)容
?? ??? ??? ??? ?System.out.println(new String(flush,0,len));
?? ??? ??? ??? ?actualSize-=len;//還剩余多少字節(jié)需要下次讀取
?? ??? ??? ?}else {
?? ??? ??? ??? ?System.out.println(new String(flush,0,actualSize));
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?raf.close();
?? ?}

}

隨機(jī)流分塊代碼的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
义乌市| 日照市| 曲麻莱县| 洛宁县| 石首市| 丘北县| 宣汉县| 施秉县| 抚州市| 巴彦淖尔市| 冷水江市| 宜春市| 昌图县| 吐鲁番市| 密云县| 色达县| 莒南县| 紫云| 阳东县| 新郑市| 怀柔区| 桦川县| 芷江| 潢川县| 体育| 宜春市| 阳谷县| 沧源| 南康市| 两当县| 太湖县| 墨脱县| 肇州县| 龙川县| 汉寿县| 娱乐| 麦盖提县| 当涂县| 永州市| 武功县| 清河县|