java大文件的分割和合并
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.List; import java.util.Properties;/**
- SplitFile.java
- @author:<a href="mailto:bylv@isoftstone.com">bylv</a>
- @DATE:2015-9-7 上午09:27:43
- Copyright (C) 2011 ISoftStone */
/**
- 功能說明:
- @author: <a href="mailto:bylv@isoftstone.com">bylv</a>
@DATE:2015-9-7 @TIME: 上午09:27:43 */ public class SplitFile {
private static final int SIZE = 1024 * 1024;// 定義單個文件的大小這里采用1m
/**
- 功能說明:
- @param args
- @author: <a href="mailto:bylv@isoftstone.com">bylv</a>
- @throws Exception
@DATE:2015-9-7 @TIME: 上午09:27:44 */ public static void main(String[] args) throws Exception { // 拆分 // File file = new File("D:/無標題.bmp"); // splitFile(file); // 合并 File file = new File("C:/parfiles"); mergeFile(file); }
/**
- 功能說明:合并文件
- @param file
- @author: <a href="mailto:bylv@isoftstone.com">bylv</a>
- @throws Exception
- @throws FileNotFoundException
@DATE:2015-9-7 @TIME: 上午09:47:31 */ private static void mergeFile(File dir) throws Exception { // 讀取properties文件的拆分信息 File[] files = dir.listFiles(new FilenameFilter() {
@Override public boolean accept(File dir, String name) { return name.endsWith(".properties"); }
}); File file = files[0]; // 獲取該文件的信息 Properties pro = new Properties(); FileInputStream fis = new FileInputStream(file); pro.load(fis); String fileName = pro.getProperty("fileName"); int splitCount = Integer.valueOf(pro.getProperty("partCount")); if (files.length != 1) {
throw new Exception(dir + ",該目錄下沒有解析的properties文件或不唯一");
}
// 獲取該目錄下所有的碎片文件 File[] partFiles = dir.listFiles(new FilenameFilter() {
@Override public boolean accept(File dir, String name) { return name.endsWith(".part"); }
}); // 將碎片文件存入到集合中 List<FileInputStream> al = new ArrayList<FileInputStream>(); for (int i = 0; i < splitCount; i++) {
try { al.add(new FileInputStream(partFiles[i])); } catch (Exception e) { // 異常 e.printStackTrace(); }
} try {
// 構建文件流集合 Enumeration<FileInputStream> en = Collections.enumeration(al); // 將多個流合成序列流 SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream(new File(dir, fileName)); byte[] b = new byte[1024]; int len = 0; while ((len = sis.read(b)) != -1) { fos.write(b, 0, len); } fos.close(); sis.close();
} catch (Exception e) {
e.printStackTrace();
} }
/**
- 功能說明:拆分文件
- @param file
- @author: <a href="mailto:bylv@isoftstone.com">bylv</a>
@DATE:2015-9-7 @TIME: 上午09:28:58 */ private static void splitFile(File file) { try {
FileInputStream fs = new FileInputStream(file); // 定義緩沖區 byte[] b = new byte[SIZE]; FileOutputStream fo = null; int len = 0; int count = 0; /** * 切割文件時,記錄 切割文件的名稱和切割的子文件個數以方便合并 * 這個信息為了簡單描述,使用鍵值對的方式,用到了properties對象 */ Properties pro = new Properties(); // 定義輸出的文件夾路徑 File dir = new File("C:/parfiles"); // 判斷文件夾是否存在,不存在則創建 if (!dir.exists()) { dir.mkdirs(); } // 切割文件 while ((len = fs.read(b)) != -1) { fo = new FileOutputStream(new File(dir, (count++) + ".part")); fo.write(b, 0, len); fo.close(); } // 將被切割的文件信息保存到properties中 pro.setProperty("partCount", count + ""); pro.setProperty("fileName", file.getName()); fo = new FileOutputStream(new File(dir, (count++) + ".properties")); // 寫入properties文件 pro.store(fo, "save file info"); fo.close(); fs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}</pre>