您的位置:首页 > 其它

切割合并大文件

2016-07-03 22:50 232 查看
package twotext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.management.RuntimeErrorException;

public class 切割合并大文件
{
/*
1.写一个方法 ,void splitBigFile(File file , File path) ,可以把一个大文件file(1G以上)
切分成若干小文件放入path目录下 (为了方便合并文件,需要提供一个配置文件记录所有小文件的
信息和顺序)(效果如图)

2.再写一个方法,void mergerPartFile(File path , File file)
把path目录下切分后的小文件合并成完整的大文件file
*/
public static void main(String[] args) throws Exception
{
File file = new File("d:\\加农贝壳-20种投篮练习(流畅).f4v");
File path = new File("G:\\part");

//		splitBigFile(file, path);
mergerPartFile(path, file);
}

public static void  mergerPartFile(File path,File file)
{
//找到path目录下的配置文件 ,xxx.confug

File[] children = path.listFiles();
if(children ==null||children.length == 0)
{
throw new RuntimeException("找不到配置文件");

}
File configFile = null;
for(File child :children)
{
if(child.isFile()&&child.getName().endsWith(".config"))
{
configFile = child;
break;
}
}

//读取配置信息
FileReader reader = null;
BufferedReader bufferedReader = null;

//从小文件中读取数据
FileInputStream in = null;
FileOutputStream out = null;//把读到的数据,写入大文件

try
{
reader = new FileReader(configFile);
bufferedReader = new BufferedReader(reader);

out = new FileOutputStream(file);

String line = null;
while((line = bufferedReader.readLine())!=null)
{
//小文件名称 line
in = new FileInputStream(new File(path,line));

//文件数据的复制
byte[] buff = new byte[1024*1024];
int len = 0;

while((len=in.read(buff))!=-1)
{
out.write(buff,0,len);
}
in.close();
}

} catch (FileNotFoundException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
finally
{
if(out!=null)
{
try
{
out.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(bufferedReader!=null)
{
try
{
bufferedReader.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(reader!=null)
{
try
{
reader.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}	if(in!=null)
{
try
{
in.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}

}

public static void splitBigFile(File file,File path) throws Exception
{
//默认小文件的长度是100M
long partFileLength = 1024*1024*100;

//参数的有效性检查
if(file==null)
{
throw new IllegalArgumentException("file不能是null");
}
if(!file.exists())
{
throw new IllegalArgumentException("file不存在");
}
if(file.isDirectory())
{
throw new IllegalArgumentException("file不是文件");
}
if(path==null)
{
throw new IllegalArgumentException("path不能是null");
}
if(!path.exists())
{
path.mkdirs();
}
if(path.isFile())
{
throw new IllegalArgumentException("path不能是文件");
}
//文件切割:从大文件中读取数据,写入到多个小文件中
FileInputStream in = null;
FileOutputStream out = null;
FileWriter configWriter = null;

String bigFilename = file.getName();
//big.txt.1.part
int index = 1;

try
{
in = new FileInputStream(file);
//第一个小文件的输出流
out = new FileOutputStream(new File(path,bigFilename+"."+index+".part"));

configWriter = new FileWriter(new File(path,bigFilename+".config"));
configWriter.write(bigFilename+"."+index+".part\r\n");

index++;
//文件数据复制
byte[] buff = new byte[1024*128];//128kb
int len = 0;

//每个小文件已经写入数据长度
long totalLength = 0;
while(true)
{
len = in.read(buff);
if(len == -1)
{
break;
}
out.write(buff,0,len);
totalLength+=len;
if(totalLength>=partFileLength)
{
//如果当前小文件的数据已经超过或等于默认小文件的大小,就准备写入下一个小文件

out.close();

out = new FileOutputStream(new File(path,bigFilename+"."+index+".part"));
configWriter.write(bigFilename+"."+index+".part\r\n");

index++;
totalLength = 0;

}
}
}
finally{
if(in!=null)
{
try
{
in.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(out!=null)
{
try
{
out.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(configWriter!=null)
{
try
{
configWriter.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: