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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hhmm665544 中级黑马   /  2014-3-13 15:45  /  977 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class SplitFile
  2. {
  3.         public static void main(String[] args)throws IOException
  4.         {
  5.    splitFile();
  6.         }
  7.        
  8.         public static void splitFile() throws IOException
  9.         {
  10.                 FileInputStream fis = new FileInputStream("c:\\1.exe");
  11.                 FileOutputStream fos = null;
  12.                
  13.                 byte[] buf = new byte[1024*1024];//建立一个大小为1Mb的缓冲区
  14.                 int len = 0;
  15.                 int count = 1;
  16.                 while((len=fis.read(buf))!=-1)
  17.                 {
  18.                         fos = new FileOutputStream("c:\\splitfiles\\"+count+++".part");
  19.                         fos.write(buf,0,len);
  20.                         fos.close();
  21.                 }
  22.                 fis.close();
  23.         }
  24. }
复制代码
如何实现大文件切割?如果是一个2G的文件,我要切成2个1G的怎么实现呢?

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

2 个回复

正序浏览
  1. import java.io.*;

  2. class Split
  3. {
  4.         public static void main(String[] args)throws IOException
  5.         {
  6.                 splitFile();
  7.         }
  8.         
  9.         public static void splitFile() throws IOException
  10.         {
  11.                 FileInputStream fis = new FileInputStream("c:\\2.exe");
  12.                 FileOutputStream fos = null;
  13.                
  14.                 byte[] buf = new byte[1024*1024];//建立一个大小为1Mb的缓冲区
  15.                 int len = 0;
  16.                 int count = 1;
  17.                 int num = 0;
  18.                 while((len=fis.read(buf))!=-1)
  19.                 {
  20.                        if(num>1023)
  21.                        {
  22.                                fos = new FileOutputStream("c:\\splitfiles\\"+count+++".part",true);
  23.                                num = 0;
  24.                        }
  25.                        else
  26.                        {
  27.                                fos = new FileOutputStream("c:\\splitfiles\\"+count+".part",true);
  28.                                 fos.write(buf,0,len);
  29.                                 num++;
  30.                                 fos.close();
  31.                        }
  32.                 }
  33.                 fis.close();
  34.         }
  35. }
复制代码


//谢谢zengming13的思路

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
说下我的想法吧,有个1M的缓冲区,读取数据写入到文件里面,
然后再向缓冲区里面存入数据,再把数据追加到先前的文件里面,
循环。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马