日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java文件分割器_JAVA学习课第五 — IO流程(九)文件分割器合成器

發布時間:2024/3/26 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件分割器_JAVA学习课第五 — IO流程(九)文件分割器合成器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件分割器

private static final int SIZE = 1024 *1024;

public static void splitFile(File file) throws IOException{

//用讀取流關聯文件(不確定文件格式)

FileInputStream fis = new FileInputStream(file);//源是一個

byte[] by = new byte[SIZE];//定義1M的緩沖區

FileOutputStream fos = null;//匯不知道有多少個

int len = 0;

int count = 1;//記錄子文件個數

File dir = new File("D:\\patFiles");

if(!dir.isFile()){

dir.mkdirs();

}

while((len = fis.read(by))!=-1){

fos = new FileOutputStream(new File(dir,(count++)+".part"));//自己定義文件格式

fos.write(by,0,len);

}

fos.close();

fis.close();

}

文件合并

public static void main(String[] args) throws IOException {

File file = new File("D:\\PartFile");

Merge(file);

}

public static void Merge(File dir)throws IOException{

ArrayList AL = new ArrayList();

for(int i = 1;i<=7;i++){

AL.add(new FileInputStream(new File(dir,i+".part")));

}

Enumeration en = Collections.enumeration(AL);

SequenceInputStream sis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream(new File(dir,"盛夏光年.mp3"));

byte[] by = new byte[1024];

int len = 0;

while((len = sis.read(by))!=-1){

fos.write(by, 0, len);

}

sis.close();

fos.close();

}

文件分割合并+配置文件

import java.io.*;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

import java.util.Properties;

public class Main

{

private static final int SIZE = 1024 *1024;

public static void main(String[] args) throws IOException {

File file1 = new File("d:\\NeedSplit\\盛夏光年.mp3");

File file2 = new File("D:\\PartFiles");

splitFile(file1);

Merge_1(file2);

}

public static void splitFile(File file) throws IOException{

//用讀取流關聯文件(不確定文件格式)

FileInputStream fis = new FileInputStream(file);//源是一個

byte[] by = new byte[SIZE];//定義1M的緩沖區

FileOutputStream fos = null;//匯不知道有多少個

int len = 0;

int count = 1;//記錄子文件個數

/*分割文件必需要記錄分割文件的名稱和分割處理的碎片文件的個數,方便合并

* 這個信息為了進行描寫敘述,使用鍵值對的方法。所以使用Properties對象*/

Properties pro = new Properties();

File dir = new File("D:\\PartFiles");

if(!dir.isFile()){

dir.mkdirs();

}

while((len = fis.read(by))!=-1){

fos = new FileOutputStream(new File(dir,(count++)+".part"));//自己定義文件格式

fos.write(by,0,len);

fos.close();

}

//將分割后文件的信息保存在pro集合中

pro.setProperty("partCount", count+"");

pro.setProperty("fileName", file.getName());

fos = new FileOutputStream(new File(dir,count+".properties"));

//將pro集合的信息存儲在集合中

pro.store(fos, "save file infmation");

fis.close();

}

public static void Merge_1(File dir)throws IOException{

//獲取指定文件夾下配置文件對象

File[] files = dir.listFiles(new SuffixFilter(".properties"));//new一個過濾器

if(files.length!=1){

throw new RuntimeException(dir+"該文件夾下沒有properties擴展名的文件或者不唯一 ");

}

//記錄配置文件對象

File confile = files[0];

//獲取配置文件信息

Properties pro = new Properties();

FileInputStream fis = new FileInputStream(confile);//關聯流對象

pro.load(fis);//載入信息

String filename = pro.getProperty("fileName");//得到文件名稱

int count = Integer.parseInt(pro.getProperty("partCount"));//得到碎片個數

//獲取該文件夾下的全部碎片文件

//定義過濾器。推斷碎片文件的個數與配置信息中的碎片信息是否一致

File[] partFiles = dir.listFiles(new SuffixFilter(".part"));

if(partFiles.length!=(count-1)){

throw new RuntimeException("碎片文件個數不正確,應是"+count+"個!");

}

//將碎片文件和流對象關聯。并存儲集合中

ArrayList AL = new ArrayList();

for(int i = 0;i

AL.add(new FileInputStream(partFiles[i]));

}

//將多個流合并成一個序列流

Enumeration en = Collections.enumeration(AL);

SequenceInputStream sis = new SequenceInputStream(en);

//讀寫過程

FileOutputStream fos = new FileOutputStream(new File(dir,filename));

byte[] by = new byte[1024];

int len = 0;

while((len = sis.read(by))!=-1){

fos.write(by, 0, len);

}

sis.close();

fos.close();

}

}

版權聲明:本文博主原創文章。博客,未經同意不得轉載。

總結

以上是生活随笔為你收集整理的java文件分割器_JAVA学习课第五 — IO流程(九)文件分割器合成器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。