java io大文件_JavaIO流对大文件进行分割与合并
對于大文件傳輸不方便時候可以試一下分割之后再操作:
package com.lym;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 1、實現一個文件分割器,把一個大文件分割成若干個小文件(可根據情況自行設計),
* 分割后的文件擴展名為dat,文件名為:原文件名+原擴展名+編號+.dat
* 2、把分割后的文件再合并(文件還原)成完整文件,與源文件一致。
* @author lym
*
*/
public class Homework2 {
/**
* 文件分割
* @param src 源文件路徑
* @param fileSize 分割后每個文件的大小,單位是MB
* @param dest 目標文件路徑
*/
public static void split(String src,int fileSize,String dest){
if("".equals(src)||src==null||fileSize==0||"".equals(dest)||dest==null){
System.out.println("分割失敗");
}
File srcFile = new File(src);//源文件
long srcSize = srcFile.length();//源文件的大小
long destSize = 1024*1024*fileSize;//目標文件的大小(分割后每個文件的大小)
int number = (int)(srcSize/destSize);
number = srcSize%destSize==0?number:number+1;//分割后文件的數目
String fileName = src.substring(src.lastIndexOf("\\"));//源文件名
InputStream in = null;//輸入字節流
BufferedInputStream bis = null;//輸入緩沖流
byte[] bytes = new byte[1024*1024];//每次讀取文件的大小為1MB
int len = -1;//每次讀取的長度值
try {
in = new FileInputStream(srcFile);
bis = new BufferedInputStream(in);
for(int i=0;i
String destName = dest+File.separator+fileName+"-"+i+".dat";
OutputStream out = new FileOutputStream(destName);
BufferedOutputStream bos = new BufferedOutputStream(out);
int count = 0;
while((len = bis.read(bytes))!=-1){
bos.write(bytes, 0, len);//把字節數據寫入目標文件中
count+=len;
if(count>=destSize){
break;
}
}
bos.flush();//刷新
bos.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//關閉流
try {
if(bis!=null)bis.close();
if(in!=null)in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件合并
* 注意:在拼接文件路勁時,一定不要忘記文件的跟路徑,否則復制不成功
* @param destPath 目標目錄
* @param srcPaths 源文件目錄
*/
public static void merge(String destPath,String ... srcPaths){
if(destPath==null||"".equals(destPath)||srcPaths==null){
System.out.println("合并失敗");
}
for (String string : srcPaths) {
if("".equals(string)||string==null)
System.out.println("合并失敗");
}
//合并后的文件名
String name = srcPaths[0].substring(srcPaths[0].lastIndexOf("\\"));
String destName = name.substring(0, name.lastIndexOf("-"));
destPath = destPath+destName;//合并后的文件路徑
File destFile = new File(destPath);//合并后的文件
OutputStream out = null;
BufferedOutputStream bos = null;
try {
out = new FileOutputStream(destFile);
bos = new BufferedOutputStream(out);
for (String src : srcPaths) {
File srcFile = new File(src);
InputStream in = new FileInputStream(srcFile);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[1024*1024];
int len = -1;
while((len = bis.read(bytes))!=-1){
bos.write(bytes, 0, len);
}
bis.close();
in.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//關閉流
try {
if(bos!=null)bos.close();
if(out!=null)out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
/**
* 分割測試
*/
// String src = "E:\\API\\JDK_API_1_6_zh_CN.CHM";//要分割的大文件
// int fileSize = 10;
// String dest = "D:\\";//文件分割后保存的路徑
// System.out.println("分割開始。。。");
// split(src, fileSize, dest);
// System.out.println("分割完成");
/**
* 合并測試
*/
//合并后文件的保存路徑
String destPath = "D:\\upan";
//要合并的文件路徑
String[] srcPaths = {
"D:\\JDK_API_1_6_zh_CN.CHM-0.dat",
"D:\\JDK_API_1_6_zh_CN.CHM-1.dat",
"D:\\JDK_API_1_6_zh_CN.CHM-2.dat",
"D:\\JDK_API_1_6_zh_CN.CHM-3.dat"};
System.out.println("開始合并。。。");
merge(destPath, srcPaths);
System.out.println("合并結束");
}
}
總結
以上是生活随笔為你收集整理的java io大文件_JavaIO流对大文件进行分割与合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java线程三部分_java 多线程三
- 下一篇: sequelize 外键关联_mysql