用filechannel更方便一些
/**
Copy files, using two techniques, FileChannels and streams.
Using FileChannels is usually faster than using streams.
*/
public final class CopyFiles {
/* Change these settings before running this class. */
/** The file to be copied. */
public static final String INPUT_FILE = "C:\\TEMP\\cottage.jpg";
/**
The name of the copy to be created by this class.
If this file doesn't exist, it will be created, along with any
needed parent directories.
*/
public static final String COPY_FILE_TO = "C:\\TEMP10\\cottage_2.jpg";
/** Run the example. */
public static void main(String... aArgs) throws IOException{
File source = new File(INPUT_FILE);
File target = new File(COPY_FILE_TO);
CopyFiles test = new CopyFiles();
test.copyWithChannels(source, target, false);
//test.copyWithStreams(source, target, false);
log("Done.");
}
/** This may fail for VERY large files. */
private void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend) {
log("Copying files with channels.");
ensureTargetDirectoryExists(aTargetFile.getParentFile());
FileChannel inChannel = null;
FileChannel outChannel = null;
FileInputStream inStream = null;
FileOutputStream outStream = null;
try{
try {
inStream = new FileInputStream(aSourceFile);
inChannel = inStream.getChannel();
outStream = new FileOutputStream(aTargetFile, aAppend);
outChannel = outStream.getChannel();
long bytesTransferred = 0;
//defensive loop - there's usually only a single iteration :
while(bytesTransferred < inChannel.size()){
bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel);
}
}
finally {
//being defensive about closing all channels and streams
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
if (inStream != null) inStream.close();
if (outStream != null) outStream.close();
}
}
catch (FileNotFoundException ex){
log("File not found: " + ex);
}
catch (IOException ex){
log(ex);
}
}
private void copyWithStreams(File aSourceFile, File aTargetFile, boolean aAppend) {
log("Copying files with streams.");
ensureTargetDirectoryExists(aTargetFile.getParentFile());
InputStream inStream = null;
OutputStream outStream = null;
try{
try {
byte[] bucket = new byte[32*1024];
inStream = new BufferedInputStream(new FileInputStream(aSourceFile));
outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend));
int bytesRead = 0;
while(bytesRead != -1){
bytesRead = inStream.read(bucket); //-1, 0, or more
if(bytesRead > 0){
outStream.write(bucket, 0, bytesRead);
}
}
}
finally {
if (inStream != null) inStream.close();
if (outStream != null) outStream.close();
}
}
catch (FileNotFoundException ex){
log("File not found: " + ex);
}
catch (IOException ex){
log(ex);
}
}
private void ensureTargetDirectoryExists(File aTargetDir){
if(!aTargetDir.exists()){
aTargetDir.mkdirs();
}
}
private static void log(Object aThing){
System.out.println(String.valueOf(aThing));
}
}
package easyway.tbs.file.transport.core.combine;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 文件合并器
* 1.首先将根据消息中线程数,过滤目录文件数目等于线程数表示文件下载完毕。
* 2.开始合并临时文件为一个文件。
*
* @author longgangbai
*
*/
public class FileCombineFetch extends Thread {
private static final transient Logger LOG = LoggerFactory.getLogger(FileCombineFetch.class);
/**
* 目标文件的名称
*/
private File targetFile;
/**
* 临时文件的目录
*/
private Collection<File> tmpFiles;
/**
* 将需要合并的文件合并为一个文件
*
* @param targetFile
* 需要生成的目标文件
* @param tmpFiles
* 临时文件的集合
*/
public FileCombineFetch(String targetFile,Collection<File> tmpFiles){
this.targetFile=new File(targetFile);
this.tmpFiles=tmpFiles;
}
/**
* 执行合并文件的线程方法
*
*/
public void run(){
BufferedOutputStream outputStream=null;
try {
//创建读取文件流的目录
outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
List<File> fileList=new ArrayList<File>();
fileList.addAll(fileList);
// Collections.sort(fileList,new Comparator<File>(){
// public int compare(File file0, File file1) {
//
// String fileIndex0=FilenameUtils.getBaseName(file0.getName()).split("_")[2];
// String fileIndex1=FilenameUtils.getBaseName(file1.getName()).split("_")[2];
// return Integer.valueOf(fileIndex0).compareTo(Integer.valueOf(fileIndex1));
// }
// });
for(int i=1;i<=tmpFiles.size();i++){
String tempFileName=targetFile.getParent()+File.separator+FilenameUtils.getBaseName(targetFile.getName())+"_"+tmpFiles.size()+"_"+i+".tmp";
File tempFile=new File(tempFileName);
System.out.println("tempFile="+tempFile.getAbsolutePath());
//读取文件的中信息
byte[] bytes=FileUtils.readFileToByteArray(tempFile);
//向目标文件中写入数据
outputStream.write(bytes);
//刷新缓冲区
outputStream.flush();
}
/* for (File tempFile : tmpFiles) {
System.out.println("tempFile"+tempFile.getName());
//读取文件的中信息
byte[] bytes=FileUtils.readFileToByteArray(tempFile);
//向目标文件中写入数据
outputStream.write(bytes);
//刷新缓冲区
outputStream.flush();
}*/
//刷新缓冲区
outputStream.flush();
//临时文件写完毕之后,删除临时文件
for (File tempFile : tmpFiles) {
FileUtils.forceDelete(tempFile);
}
//向数据写日志
//介绍文件成功
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public File getTargetFile() {
return targetFile;
}
public void setTargetFile(File targetFile) {
this.targetFile = targetFile;
}
public Collection<File> getTmpFiles() {
return tmpFiles;
}
public void setTmpFiles(Collection<File> tmpFiles) {
this.tmpFiles = tmpFiles;
}
}
package easyway.tbs.file.transport.core.combine;
import static easyway.tbs.file.transport.commons.Constants.FILE_SEPARATOR_CHAR;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import easyway.tbs.commons.FileOperateUtils;
/**
* 文件合并线程
*
* @author longgangbai
*
*/
@SuppressWarnings("unchecked")
public class FileCombineManager {
private static final transient Logger LOG = LoggerFactory.getLogger(FileCombineManager.class);
/**
* 临时文件目录
*/
private String tmpdir;
/**
* 文件扩展后缀
*/
private String[] extensions;
public FileCombineManager(String tmpdir,String[] extensions){
this.tmpdir=tmpdir;
this.extensions=extensions;
}
/**
*合并文件的信息
*/
public void combineFiles(){
//创建临时目录的文件系统
File directory=new File(tmpdir);
if(directory.exists()){
//获取所有的临时文件
Collection<File> tmpFileColl=FileOperateUtils.searchExtensionFile(directory, extensions, false);
//存储文件名称的字符串集合
Map<String,Integer> fileNameMap=new HashMap<String,Integer>();
//遍历获取相关的临时文件信息
for (File file : tmpFileColl) {
String fileName=file.getName();
//临时文件命名格式:目标文件名称+“_”+线程数+"_"+"当前下载的线程号"
String prefix=fileName.split(FILE_SEPARATOR_CHAR)[0];
Integer threadNum=Integer.valueOf(fileName.split(FILE_SEPARATOR_CHAR)[1]);
if(!fileNameMap.containsKey(prefix)){
fileNameMap.put(prefix, threadNum);
}
}
//获取所有的临时文件名称
Set<Entry<String,Integer>> fileNameEntryMap=fileNameMap.entrySet();
for (Entry<String, Integer> entry : fileNameEntryMap) {
//文件的名称
final String fileName=entry.getKey();
//获取同一个文件的所有的临时文件
Collection<File> oneFileTmpColl=CollectionUtils.predicatedCollection(tmpFileColl, new Predicate(){
public boolean evaluate(Object object) {
File file=(File)object;
StringUtils.contains(file.getName(), fileName);
return true;
}
});
System.out.println("Coll ="+oneFileTmpColl);
//临时文件的总的数量
int totalTmpFile=oneFileTmpColl.size();
//检查临时文件是否现在完毕
if(totalTmpFile==entry.getValue()){
//开始执行合并工作
String targetFileName=tmpdir+File.separator+fileName+".txt";
new FileCombineFetch(targetFileName,oneFileTmpColl).start();
}
}
};
}
public String[] getExtensions() {
return extensions;
}
public void setExtensions(String[] extensions) {
this.extensions = extensions;
}
public String getTmpdir() {
return tmpdir;
}
public void setTmpdir(String tmpdir) {
this.tmpdir = tmpdir;
}
}
按 Ctrl+C 复制代码package easyway.tbs.file.transport.core.combine;
/**
* 测试代码
* @author Owner
*
*/
public class FileCombineManagerMain {
public static void main(String[] args) {
FileCombineManager fileCombine=new FileCombineManager("E:/TestJoltProxy",new String[]{"tmp"});
fileCombine.combineFiles();
}
}
按 Ctrl+C 复制代码