`
moshangchenzi
  • 浏览: 51652 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

JDK6.0学习笔记(二十)文件切割

 
阅读更多
 
  1. /**
  2.  * 文件切割
  3.  * 允许用户指定切割的尺度
  4.  * 允许用户指定切割后小文件存放的路径,同时生成起“索引”作用的 文件,以便以后合并小文件
  5.  * 切割机合并小文件
  6.  * 输入参数  切割还是合并(切割  0,合并  1)、
  7.  * 文件名(切割的文件的路径,合并,索引文件的位置) 
  8.  * 切割尺度,以字节为单位,合并时输入0
  9.  * 目标目录(切割,小文件和索引文件存放的位置,合并,大文件存放的路径)
  10.  * 如:0 GoogleEarth.exe 102400 temp\ 
  11.  *     1 temp\GoogleEarth.exe_count 0 temp\ 
  12.  *     GoogleEarth.exe_count   索引文件
  13.  * */
  14. public class FileSplit {
  15.     public static void main(String[] args) {
  16.         if (args.length != 4) {
  17.             System.out
  18.                     .println("Usage: java FileSplit CutOrLink(0/1) FileName(if link then input CountFile) UnitSize(if link then input 0) TargetDirectory");
  19.             return;
  20.         }
  21.         if (args[0].equals("0")) {
  22.             CutFile cutFile = new CutFile();
  23.             cutFile.fileName = args[1];
  24.             cutFile.unitSize = Long.parseLong(args[2]);
  25.             cutFile.targetDir = args[3];
  26.             int count = 0;
  27.             try {
  28.                 count = cutFile.cutFile();
  29.                 System.out.println("分解成" + count + "个小文件...");
  30.             } catch (Exception e) {
  31.                 e.printStackTrace();
  32.             }
  33.         }
  34.         if (args[0].equals("1")) {
  35.             LinkFile linkFile = new LinkFile();
  36.             linkFile.fileName = args[1];
  37.             linkFile.targetDir = args[3];
  38.             int count = 0;
  39.             try {
  40.                 count = linkFile.linkFile();
  41.                 System.out.println("将" + count + "个小文件合并...");
  42.             } catch (Exception e) {
  43.                 e.printStackTrace();
  44.             }
  45.         }
  46.     }
  47. }
  1. /**
  2.  * 文件切割功能类 
  3.  * */
  4. import java.io.*;
  5. public class CutFile {  //文件切割功能类
  6.     public String fileName = null;
  7.     public long unitSize = 0;
  8.     public String targetDir = null;
  9.     public int cutFile() throws Exception {
  10.         File file = new File(fileName);
  11.         long size = file.length();// 总字节数
  12.         int count = 0;// 小文件数
  13.         long pos = 0;// 当前位置
  14.         long last = 0;// 剩余字节数
  15.         DataInputStream dis = new DataInputStream(new BufferedInputStream(
  16.                 new FileInputStream(file), (int) unitSize));// 生成文件流
  17.         byte[] databuf = new byte[(int) unitSize];
  18.         while (pos < size) {
  19.             count++;
  20.             last = size - pos;
  21.             if (last < unitSize)
  22.                 databuf = new byte[(int) last];
  23.             dis.read(databuf);
  24.             System.out.println("count=" + count + ";pos=" + pos
  25.                     + ";databuf.length=" + databuf.length);
  26.             pos = pos + databuf.length;
  27.             // 写小文件
  28.             try {
  29.                 RandomAccessFile raf = new RandomAccessFile(targetDir
  30.                         + file.getName() + "_" + count, "rw");
  31.                 raf.write(databuf);
  32.                 raf.close();
  33.             } catch (Exception e) {
  34.                 throw e;
  35.             }
  36.         }
  37.         // 记载小文件数
  38.         File file1 = new File(targetDir + file.getName() + "_count");
  39.         FileWriter fw = new FileWriter(file1);
  40.         BufferedWriter bw = new BufferedWriter(fw);
  41.         String str = (new Integer(count)).toString();
  42.         bw.write(str, 0, str.length());
  43.         bw.flush();
  44.         bw.close();
  45.         // 返回
  46.         return count;
  47.     }
  48. }
  1. /**
  2.  * 文件合并功能类 
  3.  * */
  4. import java.io.*;
  5. public class LinkFile {
  6.     public String fileName = null;
  7.     public String targetDir = null;
  8.     public int linkFile() throws Exception {
  9.         // 得到小文件数
  10.         int count = 0;
  11.         try {
  12.             File countFile = new File(this.fileName);
  13.             FileReader fr = new FileReader(countFile);
  14.             BufferedReader br = new BufferedReader(fr);
  15.             br.mark(1);
  16.             count = Integer.parseInt(br.readLine());
  17.             br.close();
  18.         } catch (Exception e) {
  19.             throw e;
  20.         }
  21.         // 还原文件名和目录名
  22.         String oldFileName = null;
  23.         String countFileDir = null;
  24.         try {
  25.             String countFileName = new File(this.fileName).getName();
  26.             countFileDir = fileName.substring(0,
  27.                     this.fileName.lastIndexOf(countFileName))
  28.                     .replace('\\', '/');
  29.             oldFileName = countFileName
  30.                     .substring(0, countFileName.length() - 6);
  31.         } catch (Exception e) {
  32.             throw e;
  33.         }
  34.         // 合并小文件
  35.         FileInputStream fis = null;
  36.         byte[] data = null;
  37.         try {
  38.             FileOutputStream fos = new FileOutputStream(this.targetDir
  39.                     + oldFileName);
  40.             for (int i = 1; i <= count; i++) {
  41.                 fis = new FileInputStream(countFileDir + oldFileName + "_" + i);
  42.                 data = new byte[fis.available()];
  43.                 fis.read(data);
  44.                 fis.close();
  45.                 fos.write(data);
  46.             }
  47.             fos.close();
  48.         } catch (Exception e) {
  49.             throw e;
  50.         }
  51.         // 返回
  52.         return count;
  53.     }
  54. }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics