IO流 应用: 文件的 切割与合并

时间:2021-05-12 21:37:11
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.TreeSet;
/*
* 容量达1MB就分割文件,,最后合并
*/
public class SplitMergeFile {
public static void main(String[] args) throws FileNotFoundException {
String path = "D:abc/";
String fileName = "网络编程(UDP-聊天).avi";
//splitFile(path, fileName);
mergeFile(path, fileName);
}
//合并文件
static void mergeFile(String path,final String fileName) throws FileNotFoundException {
File f = new File(path);
File[] files = null;
if (!(f.exists() && f.isDirectory())) {
System.out.println("需要合并的文件 的 路径错误");
return;
}
files = f.listFiles();
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
/*
* 因为 这些切割文件名 是String类型,,一旦文件过多,那么这些文件的排序就是以
* 字典顺序排序,,这样加入arraylist中,就顺序错了,所以呢要定义一个
* 带比较器的 TreeSet 来保存,,最后才从ts中 迭代 加入 al中
*/
TreeSet<File> ts = new TreeSet<File>(new Comparator<File>() {

@Override
public int compare(File f1, File f2) {
// TODO Auto-generated method stub
//fileName.length() 就是切割文件中计数的开始索引,
//lastIndexOf(".part")前就是计数的结束索引
int partIndex1 = f1.getName().lastIndexOf(".part");
String count1 = f1.getName().substring(fileName.length(),partIndex1);
int partIndex2 = f2.getName().lastIndexOf(".part");
String count2 = f2.getName().substring(fileName.length(),partIndex2);
return Integer.parseInt(count1) - Integer.parseInt(count2);
}
});
for (int i = 0; i < files.length; i++) {
if (files[i].getName().startsWith(fileName) &&
files[i].getName().endsWith(".part")) {
ts.add(files[i]);
System.out.println(files[i].getName());
}
}
System.out.println();
for (File temp : ts) {
al.add(new FileInputStream(temp));
System.out.println(temp.getName());
}
final Iterator<FileInputStream> it = al.iterator();
Enumeration<FileInputStream> ef = new Enumeration<FileInputStream>() {

@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return it.next();
}

@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return it.hasNext();
}
};
//合并输入 流 SequenceInputStream
SequenceInputStream sis = new SequenceInputStream(ef);

FileOutputStream fos = new FileOutputStream(path+fileName);

byte[] buf = new byte[1024*1024];
int len = 0;
try {
while ((len = sis.read(buf)) != -1) {
fos.write(buf,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (sis!=null) {
try {
sis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//分割文件
static void splitFile(String path, String fileName) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(path+fileName);
byte[] buf = new byte[1024*1024];// 1024b是1k,1024kb 是1mb
int len = 0;
int count = 1;
while ((len = fis.read(buf)) != -1) {
fos = new FileOutputStream(path + fileName + (count++) + ".part");
fos.write(buf,0,len);
fos.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}