文件的分割与合并

package com.dt.file;


import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Set;


public class FileTest {


public static void main(String[] args) throws Exception
{
/*
*将一个文件分割为多个文件,然后再组合成一个文件
* 找到文件,读入一个1M的buffer中,然后写入一个Part文件中,循环此操作直到文件读取完毕
*/


String sourceFilePath = "E:" + File.separator + "soft" + File.separator + "apache-tomcat-7.0.62-windows-x64.zip";
int partFileLength = 1024*1024;//指定分割的子文件大小为1M
splitFile(sourceFilePath,partFileLength);//将文件分割
combineFile("D:" + File.separator + "code" + File.separator + "target");//将分割后的文件合并
}

//文件分割
public static void splitFile(String sourceFilePath,int partFileLength) throws Exception 
{
File sourceFile = null;
File targetFile = null;
InputStream ips = null;
OutputStream ops = null;
OutputStream configOps = null;//该文件流用于存储文件分割后的相关信息,包括分割后的每个子文件的编号和路径,以及未分割前文件名
Properties partInfo = null;//properties用于存储文件分割的信息
byte[] buffer = null;
int partNumber = 1;
sourceFile = new File(sourceFilePath);//待分割文件
ips = new FileInputStream(sourceFile);//找到读取源文件并获取输入流
configOps = new FileOutputStream(new File("D:" + File.separator + "code" + File.separator + "target" + File.separator + "config.properties"));//配置文件
buffer = new byte[partFileLength];//开辟缓存空间
int tempLength = 0;
partInfo = new Properties();//key:1开始自动编号 value:文件路径



// read(byte[] b, int off, int len)  off是偏移量,即从数组b的第off个位置起读取 len个字节
while((tempLength = ips.read(buffer,0,partFileLength)) != -1) // 0 是偏移量位置 第一次0 第二次 1048576
{

String targetFilePath = "D:" + File.separator + "Code" + File.separator + "target" + File.separator + "part_" + (partNumber);//分割后的文件路径+文件名
partInfo.setProperty((partNumber++)+"",targetFilePath);//将相关信息存储进properties
targetFile = new File(targetFilePath);
ops = new FileOutputStream(targetFile);//分割后文件
ops.write(buffer,0,tempLength);//将信息写入碎片文件
            System.out.println(tempLength);
ops.close();//关闭碎片文件
}
partInfo.setProperty("name",sourceFile.getName());//存储源文件名  name=apache-tomcat-7.0.62-windows-x64.zip
partInfo.store(configOps,"ConfigFile");//将properties存储进实体文件中
ips.close();//关闭源文件流
}




public static void combineFile(String directoryPath) throws Exception
{
Properties config = new Properties();
InputStream ips = null;
ips = new FileInputStream(new File(directoryPath + File.separator + "config.properties"));
config.load(ips);


Set keySet = config.keySet();//需要将keySet转换为int型


//将keySet迭代出来,转换成int类型的set,排序后存储进去
Set<Integer> intSet = new TreeSet<Integer>();
Iterator iterString = keySet.iterator();
while(iterString.hasNext())
{
String tempKey = (String)iterString.next();
if("name".equals(tempKey))
{}
else
{
System.out.println(tempKey);
int tempInt = Integer.parseInt(tempKey);
intSet.add(tempInt);
}
}

//Set<Integer> sortedKeySet = new TreeSet<Integer>();
//sortedKeySet.addAll(intSet);


OutputStream eachFileOutput = null;
eachFileOutput = new FileOutputStream(new File("D:" + File.separator + "code" + File.separator + config.getProperty("name")));


Iterator iter = intSet.iterator();
while(iter.hasNext())
{
String key = new String("" + iter.next());
if(key.equals("name"))
{}
else
{
//System.out.println("debug---");
String fileNumber = null;
String filePath = null;
fileNumber = key;
filePath = config.getProperty(fileNumber);


//循环读取文件 --> 依次写入


InputStream eachFileInput = null;

eachFileInput = new FileInputStream(new File(filePath));

byte[] buffer = new byte[1024*1024*1];//缓冲区文件大小为1M
int len = 0;
while((len = eachFileInput.read(buffer,0,1024*1024*1)) != -1)
{
eachFileOutput.write(buffer,0,len);
}
eachFileInput.close();
}
}


eachFileOutput.close();
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值