将两个文件合并到一个文件中
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
//序列流的构造函数 可以接受两个文件输入流
SequenceInputStream sis = new SequenceInputStream(f1, f2);
FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf))!=-1){
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
但是这个只能添加两个文件
如果想要操作多个需要加入枚举
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
FileInputStream f3 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
Vector<FileInputStream> v = new Vector<>();
v.add(f1);
v.add(f2);
v.add(f3);
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\four.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf))!=-1){
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
-----------------------下面的更好----------------------------------
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
FileInputStream f3 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
ArrayList<FileInputStream> a = new ArrayList<>();
a.add(f1);
a.add(f2);
a.add(f3);
Enumeration<FileInputStream> en = Collections.enumeration(a);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\four.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf))!=-1){
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
将文件分割成多个小文件碎片
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\悠悠华\\Desktop\\test\\abc.bmp");
split(file);
}
public static void split(File file) throws IOException{
//将文件分割成多个碎片
FileInputStream fis = new FileInputStream(file);
File dir = new File("C:\\Users\\悠悠华\\Desktop\\test\\split");
FileOutputStream fos = null;
//如果不存在这个文件夹就创建
if(!dir.exists())
dir.mkdirs();
//每次读取1M文件
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while((len = fis.read(buf))!=-1){
fos = new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf, 0, len);
}
//关闭流
fos.close();
fis.close();
}
}
结果:
将分割的文件合并起来
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\悠悠华\\Desktop\\test\\split");
merge(file);
}
public static void merge(File dir) throws IOException{
ArrayList<FileInputStream> a = new ArrayList<>();
for(int x = 1; x<=11;x++){
a.add(new FileInputStream(new File(dir,x+".part")));
}
Enumeration<FileInputStream> en = Collections.enumeration(a);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir,"this.bmp"));
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf))!=-1){
fos.write(buf, 0, len);
}
sis.close();
fos.close();
}
}