文件切割和文件合并

实现对大文件的合并

1、确定源和目的

2、看是否处理的数据是纯文本

3、明确数据所在的设备

4、明确是否需要额外功能(是否要高效(缓存流)、是否需要转换(转换流)、是否对基本数据类型进行操作(DataInputstream)、是否对操作对象序列化()、是否需要多个员合并、需要保证数据的表现形式到目的地吗)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import javax.swing.JFileChooser;


import org.junit.Test;


public class FileSpiltEx {
private static final String File = null;


@Test
// 演示文件切割
public void splitDemo() throws Exception {
JFileChooser jfc = new JFileChooser();
int iSelBtn = jfc.showOpenDialog(null);
if (iSelBtn == JFileChooser.APPROVE_OPTION) {
// 获取用户要切割的文件
File file = jfc.getSelectedFile();
// 在切割文件所在文件夹中创建一个"splitFiles"子文件夹,用于存储切割后的碎片
File desDir = new File(file.getParent(), "splitFiles");


fileSplit(file, desDir);
}
}


// 分割
private void fileSplit(File file, File desDir) throws IOException {
if(!desDir.exists()){
desDir.mkdir();

}
FileInputStream fin=new   FileInputStream(file);
FileOutputStream fout=null;
int len =0;
int count=1;
byte[] b=new byte[1024*1024];
while((len=fin.read(b))!=-1){
String filename=file.getName()+(count++)+".part";
fout=new FileOutputStream(new File(desDir,filename));
fout.write(b, 0, len);
}
fin.close();
}

}

猜你喜欢

转载自blog.csdn.net/qq_35307947/article/details/80573306
今日推荐