- /**
- * 文件切割
- * 允许用户指定切割的尺度
- * 允许用户指定切割后小文件存放的路径,同时生成起“索引”作用的 文件,以便以后合并小文件
- * 切割机合并小文件
- * 输入参数 切割还是合并(切割 0,合并 1)、
- * 文件名(切割的文件的路径,合并,索引文件的位置)
- * 切割尺度,以字节为单位,合并时输入0
- * 目标目录(切割,小文件和索引文件存放的位置,合并,大文件存放的路径)
- * 如:0 GoogleEarth.exe 102400 temp/
- * 1 temp/GoogleEarth.exe_count 0 temp/
- * GoogleEarth.exe_count 索引文件
- * */
- public class FileSplit {
- public static void main(String[] args) {
- if (args.length != 4) {
- System.out
- .println("Usage: java FileSplit CutOrLink(0/1) FileName(if link then input CountFile) UnitSize(if link then input 0) TargetDirectory");
- return;
- }
- if (args[0].equals("0")) {
- CutFile cutFile = new CutFile();
- cutFile.fileName = args[1];
- cutFile.unitSize = Long.parseLong(args[2]);
- cutFile.targetDir = args[3];
- int count = 0;
- try {
- count = cutFile.cutFile();
- System.out.println("分解成" + count + "个小文件...");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (args[0].equals("1")) {
- LinkFile linkFile = new LinkFile();
- linkFile.fileName = args[1];
- linkFile.targetDir = args[3];
- int count = 0;
- try {
- count = linkFile.linkFile();
- System.out.println("将" + count + "个小文件合并...");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 文件切割功能类
- * */
- import java.io.*;
- public class CutFile { //文件切割功能类
- public String fileName = null;
- public long unitSize = 0;
- public String targetDir = null;
- public int cutFile() throws Exception {
- File file = new File(fileName);
- long size = file.length();// 总字节数
- int count = 0;// 小文件数
- long pos = 0;// 当前位置
- long last = 0;// 剩余字节数
- DataInputStream dis = new DataInputStream(new BufferedInputStream(
- new FileInputStream(file), (int) unitSize));// 生成文件流
- byte[] databuf = new byte[(int) unitSize];
- while (pos < size) {
- count++;
- last = size - pos;
- if (last < unitSize)
- databuf = new byte[(int) last];
- dis.read(databuf);
- System.out.println("count=" + count + ";pos=" + pos
- + ";databuf.length=" + databuf.length);
- pos = pos + databuf.length;
- // 写小文件
- try {
- RandomAccessFile raf = new RandomAccessFile(targetDir
- + file.getName() + "_" + count, "rw");
- raf.write(databuf);
- raf.close();
- } catch (Exception e) {
- throw e;
- }
- }
- // 记载小文件数
- File file1 = new File(targetDir + file.getName() + "_count");
- FileWriter fw = new FileWriter(file1);
- BufferedWriter bw = new BufferedWriter(fw);
- String str = (new Integer(count)).toString();
- bw.write(str, 0, str.length());
- bw.flush();
- bw.close();
- // 返回
- return count;
- }
- }
- /**
- * 文件合并功能类
- * */
- import java.io.*;
- public class LinkFile {
- public String fileName = null;
- public String targetDir = null;
- public int linkFile() throws Exception {
- // 得到小文件数
- int count = 0;
- try {
- File countFile = new File(this.fileName);
- FileReader fr = new FileReader(countFile);
- BufferedReader br = new BufferedReader(fr);
- br.mark(1);
- count = Integer.parseInt(br.readLine());
- br.close();
- } catch (Exception e) {
- throw e;
- }
- // 还原文件名和目录名
- String oldFileName = null;
- String countFileDir = null;
- try {
- String countFileName = new File(this.fileName).getName();
- countFileDir = fileName.substring(0,
- this.fileName.lastIndexOf(countFileName))
- .replace('//', '/');
- oldFileName = countFileName
- .substring(0, countFileName.length() - 6);
- } catch (Exception e) {
- throw e;
- }
- // 合并小文件
- FileInputStream fis = null;
- byte[] data = null;
- try {
- FileOutputStream fos = new FileOutputStream(this.targetDir
- + oldFileName);
- for (int i = 1; i <= count; i++) {
- fis = new FileInputStream(countFileDir + oldFileName + "_" + i);
- data = new byte[fis.available()];
- fis.read(data);
- fis.close();
- fos.write(data);
- }
- fos.close();
- } catch (Exception e) {
- throw e;
- }
- // 返回
- return count;
- }
- }