黑马程序员技术交流社区
标题:
如何实现大文件切割
[打印本页]
作者:
hhmm665544
时间:
2014-3-13 15:45
标题:
如何实现大文件切割
class SplitFile
{
public static void main(String[] args)throws IOException
{
splitFile();
}
public static void splitFile() throws IOException
{
FileInputStream fis = new FileInputStream("c:\\1.exe");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];//建立一个大小为1Mb的缓冲区
int len = 0;
int count = 1;
while((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream("c:\\splitfiles\\"+count+++".part");
fos.write(buf,0,len);
fos.close();
}
fis.close();
}
}
复制代码
如何实现大文件切割?如果是一个2G的文件,我要切成2个1G的怎么实现呢?
作者:
李白衣
时间:
2014-3-13 16:04
说下我的想法吧,有个1M的缓冲区,读取数据写入到文件里面,
然后再向缓冲区里面存入数据,再把数据追加到先前的文件里面,
循环。
作者:
hhmm665544
时间:
2014-3-13 17:00
import java.io.*;
class Split
{
public static void main(String[] args)throws IOException
{
splitFile();
}
public static void splitFile() throws IOException
{
FileInputStream fis = new FileInputStream("c:\\2.exe");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];//建立一个大小为1Mb的缓冲区
int len = 0;
int count = 1;
int num = 0;
while((len=fis.read(buf))!=-1)
{
if(num>1023)
{
fos = new FileOutputStream("c:\\splitfiles\\"+count+++".part",true);
num = 0;
}
else
{
fos = new FileOutputStream("c:\\splitfiles\\"+count+".part",true);
fos.write(buf,0,len);
num++;
fos.close();
}
}
fis.close();
}
}
复制代码
//谢谢zengming13的思路
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2