package com.lxb.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
public class SplitFileDemo {
private static final int SIZE=1024*1024;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("C:\\Users\\xiubin\\Desktop\\eop\\jay_01.mp3");
File file2=new File("E:\\partFiles");
/**
* 切割文件
*/
// splitFile(file);
/**
* 文件合并
*/
// mergeFile(file2);
/**
* 文件切割合并+配置文件
*/
splitFile_02(file);
mergeFile_02(file2);
}
private static void mergeFile_02(File dir) throws IOException {
// TODO Auto-generated method stub
File[] files=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".properties");
}
});
if(files.length!=1)
throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一");
Properties prop=new Properties();
FileInputStream fis=new FileInputStream(files[0]);
prop.load(fis);
List<FileInputStream> al=new ArrayList<FileInputStream>();
String filename=prop.getProperty("filename");
int count=Integer.valueOf(prop.getProperty("part"));
File[] files2=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".part");
}
});
if(files2.length>0) {
for(int x=1;x<=count-1;x++) {
al.add(new FileInputStream(new File(dir,x+".part")));
}
Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,"1.mp3"));
byte[] buf=new byte[SIZE];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}else {
throw new RuntimeException("碎片文件不符合要求,个数不对,应该"+count+"个");
}
}
private static void splitFile_02(File file) throws IOException {
// TODO Auto-generated method stub
/**
* 用读取流关联源文件
*/
FileInputStream fis=new FileInputStream(file);
/**
* 定义一个1M缓冲区
*/
byte[] buf=new byte[SIZE];
/**
* 创建目的
*/
FileOutputStream fos=null;
int len=0;
int count=1;
/**
* 切割文件,必须记录住被切割文件的名称,以及切割出来碎片文件的个数,以方便合并
* 这个信息为了进行简单的描述,使用键值对方式,用到了properties对象
*
*/
Properties prop=new Properties();
File dir=new File("e:\\partFiles");
if(!dir.exists())
dir.mkdirs();
while((len=fis.read(buf))!=-1) {
fos=new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf, 0, len);
fos.close();
}
/**
* 将被切割文件的信息保存到prop集合中
*/
prop.setProperty("part", count+"");
prop.setProperty("filename", file.getName());
fos=new FileOutputStream(new File(dir,count+".properties"));
//保存到prop文本
prop.store(fos, "save file info");
fos.close();
fis.close();
}
public static void splitFile(File file) throws IOException {
/**
* 用读取流关联源文件
*/
FileInputStream fis=new FileInputStream(file);
/**
* 定义一个1M缓冲区
*/
byte[] buf=new byte[SIZE];
/**
* 创建目的
*/
FileOutputStream fos=null;
int len=0;
int count=1;
File dir=new File("e:\\partFiles");
if(!dir.exists())
dir.mkdirs();
while((len=fis.read(buf))!=-1) {
fos=new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf, 0, len);
fos.close();
}
fis.close();
}
public static void mergeFile(File dir) throws IOException {
List<FileInputStream> al=new ArrayList<FileInputStream>();
for(int x=1;x<=4;x++) {
al.add(new FileInputStream(new File(dir,x+".part")));
}
Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,"1.mp3"));
byte[] buf=new byte[SIZE];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
public class SplitFileDemo {
private static final int SIZE=1024*1024;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("C:\\Users\\xiubin\\Desktop\\eop\\jay_01.mp3");
File file2=new File("E:\\partFiles");
/**
* 切割文件
*/
// splitFile(file);
/**
* 文件合并
*/
// mergeFile(file2);
/**
* 文件切割合并+配置文件
*/
splitFile_02(file);
mergeFile_02(file2);
}
private static void mergeFile_02(File dir) throws IOException {
// TODO Auto-generated method stub
File[] files=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".properties");
}
});
if(files.length!=1)
throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一");
Properties prop=new Properties();
FileInputStream fis=new FileInputStream(files[0]);
prop.load(fis);
List<FileInputStream> al=new ArrayList<FileInputStream>();
String filename=prop.getProperty("filename");
int count=Integer.valueOf(prop.getProperty("part"));
File[] files2=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".part");
}
});
if(files2.length>0) {
for(int x=1;x<=count-1;x++) {
al.add(new FileInputStream(new File(dir,x+".part")));
}
Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,"1.mp3"));
byte[] buf=new byte[SIZE];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}else {
throw new RuntimeException("碎片文件不符合要求,个数不对,应该"+count+"个");
}
}
private static void splitFile_02(File file) throws IOException {
// TODO Auto-generated method stub
/**
* 用读取流关联源文件
*/
FileInputStream fis=new FileInputStream(file);
/**
* 定义一个1M缓冲区
*/
byte[] buf=new byte[SIZE];
/**
* 创建目的
*/
FileOutputStream fos=null;
int len=0;
int count=1;
/**
* 切割文件,必须记录住被切割文件的名称,以及切割出来碎片文件的个数,以方便合并
* 这个信息为了进行简单的描述,使用键值对方式,用到了properties对象
*
*/
Properties prop=new Properties();
File dir=new File("e:\\partFiles");
if(!dir.exists())
dir.mkdirs();
while((len=fis.read(buf))!=-1) {
fos=new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf, 0, len);
fos.close();
}
/**
* 将被切割文件的信息保存到prop集合中
*/
prop.setProperty("part", count+"");
prop.setProperty("filename", file.getName());
fos=new FileOutputStream(new File(dir,count+".properties"));
//保存到prop文本
prop.store(fos, "save file info");
fos.close();
fis.close();
}
public static void splitFile(File file) throws IOException {
/**
* 用读取流关联源文件
*/
FileInputStream fis=new FileInputStream(file);
/**
* 定义一个1M缓冲区
*/
byte[] buf=new byte[SIZE];
/**
* 创建目的
*/
FileOutputStream fos=null;
int len=0;
int count=1;
File dir=new File("e:\\partFiles");
if(!dir.exists())
dir.mkdirs();
while((len=fis.read(buf))!=-1) {
fos=new FileOutputStream(new File(dir,(count++)+".part"));
fos.write(buf, 0, len);
fos.close();
}
fis.close();
}
public static void mergeFile(File dir) throws IOException {
List<FileInputStream> al=new ArrayList<FileInputStream>();
for(int x=1;x<=4;x++) {
al.add(new FileInputStream(new File(dir,x+".part")));
}
Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,"1.mp3"));
byte[] buf=new byte[SIZE];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
}