文件切割
1. 按文件大小切割
- 创建字节流输入对象读取源文件
- 设置缓冲区大小(即要分割的独立文件大小)
- 创建字节流输出对象将缓冲区的数据写入到单个文件中
package demo1;
import java.io.*;
public class SplitFileTest {
private static final int BUFFER_SIZE = 1024*200;
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) throws IOException {
File srcFile = new File("D:\\code_test3\\Lighthouse.jpg");
File partsDir = new File("D:\\code_test4");
splitFile(srcFile, partsDir);
}
public static void splitFile(File srcFile, File partsDir)
throws IOException {
if (!(srcFile.exists() && srcFile.isFile())) {
throw new RuntimeException("源文件不是正确的文件或者不存在");
}
if (!partsDir.exists()) {
partsDir.mkdirs();
}
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = null;
byte[] buf = new byte[BUFFER_SIZE];
int len = 0;
int count = 1;
while ((len = fis.read(buf)) != -1) {
fos = new FileOutputStream(new File(partsDir, (count++) + ".part"));
fos.write(buf, 0, len);
fos.close();
}
String filename = srcFile.getName();
int partCount = count;
fos = new FileOutputStream(new File(partsDir, count + ".properties"));
fos.write(("filename=" + filename + LINE_SEPARATOR).getBytes());
fos.write(("partCount=" + Integer.toString(partCount)).getBytes());
fos.close();
fis.close();
}
}
- 注意:该程序执行后会生成一个
properties
配置文件,配置文件中存放源文件的名称、后缀以及分割的文件个数。使用properties文件存放信息利于对文件的合并。
2. 按指定数量切割
文件合成
- 对一个文件进行切割(一个源对应多个目的),切成碎片,在将碎片进行合并成原来的文件。
package demo1;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
public class MergerFileTest3 {
public static void main(String[] args) throws IOException{
File partdir = new File("D:\\code_test4");
mergerFile(partdir);
}
public static void mergerFile(File partsDir) throws IOException{
File configFile = getConfigFile(partsDir);
Properties prop = getProperties(configFile);
merge(partsDir,prop);
}
public static Properties getProperties(File configFile) throws IOException {
FileInputStream fis = null;
Properties prop = new Properties();
try {
fis = new FileInputStream(configFile);
prop.load(fis);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
System.err.println("err");
}
}
}
return prop;
}
private static File getConfigFile(File dir) {
if (!dir.exists() && dir.isDirectory()) {
throw new RuntimeException("文件路径异常");
}
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".properties");
}
});
if (files.length != 1) {
throw new RuntimeException("配置文件不存在");
}
File congfigFile = files[0];
return congfigFile;
}
private static void merge (File dir , Properties prop) throws IOException{
String filename = prop.getProperty("filename");
int partCount = Integer.parseInt(prop.getProperty("partCount"));
ArrayList<FileInputStream> list = new ArrayList<>();
for (int i = 1; i < partCount; i++) {
list.add(new FileInputStream(new File(dir,i +".part")));
}
Enumeration<FileInputStream> en = Collections.enumeration(list);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir, filename));
byte[] buf = new byte[1024];
int len;
while ((len = sis.read()) != -1) {
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}