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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 少年闰土 于 2015-6-9 22:59 编辑
  1. import java.io.*;

  2. public class SplitFile {

  3.         public static void main(String[] args) {
  4.                 splitFile("F:\\陈奕迅 - 十年.mp3", 1);

  5.         }
  6.         


  7.         /**
  8.          * @param fileName:文件名
  9.          * @param length:要分割的长度,以M为单位
  10.          */
  11.         static void splitFile(String fileName, long length) {
  12.                 final long SIZE = length * 1024 *1024;
  13.                 int count = 1;
  14.                 int c;
  15.                 long len = 0;
  16.                 BufferedInputStream bufInS = null;
  17.                 BufferedOutputStream bufOutS = null;
  18.                
  19.                 File file = new File(fileName);
  20.                 File fileAbs = file.getAbsoluteFile(); //获取绝对路径
  21.                 String parent = fileAbs.getParent(); //获取要分割文件所在目录
  22.                
  23.                 try {
  24.                         bufInS = new BufferedInputStream(new FileInputStream(fileAbs)); //创建要分割文件的输入流
  25.                         bufOutS = new BufferedOutputStream(new FileOutputStream(parent + count + ".part")); //创建分割后的文件第一部分
  26.                         
  27.                         while ((c = bufInS.read()) != -1) {
  28.                                 len++;
  29.                                 if (len > SIZE) { //前一部分文件写满,重新创建一个文件
  30.                                         len = 0;
  31.                                         count++;
  32.                                         bufOutS.close(); //关闭前一部分文件的输出流
  33.                                         bufOutS = new BufferedOutputStream(new FileOutputStream(parent + count + ".part")); //创建新文件的输出流
  34.                                 }
  35.                                 bufOutS.write(c); //给文件中写内容
  36.                         }        
  37.                 } catch (FileNotFoundException e) {
  38.                         // TODO 自动生成的 catch 块
  39.                         e.printStackTrace();
  40.                 } catch (IOException e) {
  41.                         e.printStackTrace();
  42.                 } finally {
  43.                         try {
  44.                                 if (bufInS != null) {
  45.                                         bufInS.close();
  46.                                 }
  47.                         } catch (IOException e) {
  48.                                 e.printStackTrace();
  49.                         }
  50.                         try {
  51.                                 if (bufOutS != null) {
  52.                                         bufOutS.close();
  53.                                 }
  54.                         } catch (IOException e) {
  55.                                 e.printStackTrace();
  56.                         }
  57.                 }
  58.                
  59.         }

  60. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马