zoukankan      html  css  js  c++  java
  • javase文件切割及融合

      1 package com.split;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.FileOutputStream;
      7 
      8 public class FileSplit {
      9 
     10     public static void main(String[] args) {
     11         // TODO Auto-generated method stub
     12         //String file = "D:\arch\d.jpg";
     13         String file="D:\arch\b.txt";
     14         split(file);
     15         String s1="D:\arch\b.txtpart-0";
     16         String s2="D:\arch\b.txtpart-1";
     17         String s3="D:\arch\b.txtpart-2";
     18         fuse(s1,s2,s3);
     19 
     20     }
     21 /**
     22  * 文件融合
     23  * @param srcFile
     24  */
     25 public static void fuse(String... srcFile ){
     26     for (String string : srcFile) {
     27         fusion(string);
     28     }
     29 }
     30     private static void fusion(String file) {
     31         File f=new File(file);
     32         FileInputStream fis=null;
     33         FileOutputStream fos=null;
     34         try {
     35             if(f.exists()){
     36                 if(f.isFile()){
     37                     fis=new FileInputStream(f);
     38                     //string="D:\arch\b.txtpart-0"
     39                     String srcPath=f.getAbsolutePath();
     40                     String sep="";
     41                     if(srcPath.contains("\")){
     42                         sep="\";
     43                     }
     44                     int index=srcPath.lastIndexOf(sep);
     45                     
     46                     //String s=srcPath.substring(0, (srcPath.length()-6));
     47                     //获取最后一个名字的字符串
     48                     String s=srcPath.substring(0, index+f.getName().length());
     49                     fos=new FileOutputStream(new File(s),true);
     50                     byte[] buf=new byte[1024];
     51                     int len=0;
     52                     while((len=fis.read(buf))!=-1){
     53                         fos.write(buf, 0, len);
     54                         fos.close();
     55                     }
     56                 }
     57             }
     58             fis.close();
     59         } catch (Exception e) {
     60             // TODO: handle exception
     61         }
     62 }
     63 /**
     64  * 文件的切割
     65  */
     66     public static void split(String srcfile) {
     67 
     68         File f = new File(srcfile);
     69         FileOutputStream fos = null;
     70         try {
     71             FileInputStream fis = new FileInputStream(f);
     72             // 获取文件大小
     73             int len = (int) f.length();
     74             // 切割块数
     75             int blocks = 3;
     76             // 每块的大小
     77             int blocksSize = len / blocks;
     78             int len0 = 0;
     79             byte[] buf = new byte[1024];
     80             for (int i = 0; i < blocks; i++) {
     81                 //根据源目录创建目标文件对象
     82                 fos = new FileOutputStream(new File(f.getParent(), f.getName() + "part-" + i));
     83                 // 如果是最后一块
     84                 if (i == (blocks - 1)) {
     85                     while ((len0 = fis.read(buf)) != -1) {
     86                         fos.write(buf, 0, len0);
     87                     }
     88                     fos.close();
     89                 }
     90                 // 不是最后一块
     91                 else {
     92                     // 不足缓冲区
     93                     if (blocksSize <=buf.length) {
     94                         buf = new byte[blocksSize];
     95                         fis.read(buf);
     96                         fos.write(buf);
     97                         fos.close();
     98                     }
     99                     // 超过缓冲区
    100                     else {
    101                         int count = blocksSize / buf.length;
    102                         for (int j = 0; j < count; j++) {
    103                             // 最后一次读取
    104                             if (j == (count - 1)) {
    105                                 if(blocksSize%buf.length !=0){
    106                                     buf = new byte[buf.length + (blocksSize % buf.length)];
    107                                     fis.read(buf);
    108                                     fos.write(buf);
    109                                     fos.close();
    110                                 }
    111                             } 
    112                             else {
    113                                 fis.read(buf);
    114                                 fos.write(buf);
    115                             }
    116                         }
    117                     }
    118                 }
    119             }
    120             fis.close();
    121         } catch (Exception e) {
    122             // TODO Auto-generated catch block
    123             e.printStackTrace();
    124         }
    125     }
    126 }
  • 相关阅读:
    应用程序域的用法
    WCF 暴露元数据的配置
    无svc文件发布WCF服务到IIS上
    C#输出毫秒
    [一点一滴学英语]20051017
    从其他平台转向Windows,MS提供了很多便利;那反过来呢?
    Ward Cunningham加入Eclipse?
    [导入][链接]Linux常用命令/快捷键
    [导入][号外]Oracle to buy JBoss, Zend and Sleepycat?
    [导入][链接]Google开始向Linux移植其Windows应用程序?
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6752031.html
Copyright © 2011-2022 走看看