A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ℃葫芦 中级黑马   /  2015-8-17 22:00  /  661 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.Properties;

  6. /*
  7. * 文件切割器。
  8. *
  9. *
  10. *
  11. *
  12. */

  13. public class SplitFileDemo {

  14.         private static final int SIZE = 1024 * 1024;

  15.         /**
  16.          * @param args
  17.          * @throws Exception
  18.          */
  19.         public static void main(String[] args) throws Exception {

  20.                 File file = new File("c:\\aa.mp3");

  21.                 splitFile_2(file);
  22.         }

  23.         private static void splitFile_2(File file) throws IOException {

  24.                 // 用读取流关联源文件。
  25.                 FileInputStream fis = new FileInputStream(file);

  26.                 // 定义一个1M的缓冲区。
  27.                 byte[] buf = new byte[SIZE];

  28.                 // 创建目的。
  29.                 FileOutputStream fos = null;

  30.                 int len = 0;
  31.                 int count = 1;
  32.                
  33.                
  34.                 /*
  35.                  * 切割文件时,必须记录住被切割文件的名称,以及切割出来碎片文件的个数。 以方便于合并。
  36.                  * 这个信息为了进行描述,使用键值对的方式。用到了properties对象
  37.                  *
  38.                  */
  39.                 Properties prop  = new Properties();
  40.                
  41.        

  42.                 File dir = new File("c:\\partfiles");
  43.                 if (!dir.exists())
  44.                         dir.mkdirs();

  45.                 while ((len = fis.read(buf)) != -1) {

  46.                         fos = new FileOutputStream(new File(dir, (count++) + ".part"));
  47.                         fos.write(buf, 0, len);
  48.                         fos.close();
  49.                 }
  50.                
  51.                 //将被切割文件的信息保存到prop集合中。
  52.                 prop.setProperty("partcount", count+"");
  53.                 prop.setProperty("filename", file.getName());
  54.                
  55.                
  56.                
  57.                 fos = new FileOutputStream(new File(dir,count+".properties"));
  58.                
  59.                 //将prop集合中的数据存储到文件中。
  60.                 prop.store(fos, "save file info");

  61.                 fos.close();
  62.                 fis.close();

  63.         }

  64.         public static void splitFile(File file) throws IOException {

  65.                 // 用读取流关联源文件。
  66.                 FileInputStream fis = new FileInputStream(file);

  67.                 // 定义一个1M的缓冲区。
  68.                 byte[] buf = new byte[SIZE];

  69.                 // 创建目的。
  70.                 FileOutputStream fos = null;

  71.                 int len = 0;
  72.                 int count = 1;

  73.                 File dir = new File("c:\\partfiles");
  74.                 if (!dir.exists())
  75.                         dir.mkdirs();

  76.                 while ((len = fis.read(buf)) != -1) {

  77.                         fos = new FileOutputStream(new File(dir, (count++) + ".part"));
  78.                         fos.write(buf, 0, len);
  79.                 }

  80.                 fos.close();
  81.                 fis.close();

  82.         }

  83. }
复制代码

1 个回复

倒序浏览
这是啥啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马