import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class SplitFileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/*
* 文件切割。
*
*需要配置文件记录碎片文件的个数。被切文件的名称。
*partCount = 6
*srcfilename = 0.mp3
*键值对,并持久化,需要用到Properties。
*/
File srcFile = new File("d:\\0.mp3");
File destDir = new File("d:\\partfiles");
if(!destDir.exists())
destDir.mkdir();
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while((len=fis.read(buf))!=-1){
fos = new FileOutputStream(new File(destDir,(count++)+".part"));
fos.write(buf,0,len);
fos.close();
}
File configFile = new File(destDir,count+".properties");
fos = new FileOutputStream(configFile);
Properties prop = new Properties();
prop.setProperty("partcount", Integer.toString(count-1));
prop.setProperty("srcfilename", srcFile.getName());
prop.store(fos, "");
fos.close();
fis.close();
}
}
|