- //文件切割,将MP3切成相等大小的碎片,最后一片的大小不一定相等
- public static void splitFile()throws IOException
- {
- File file = new File("F:\\Study\\day20\\02\\泡沫 - G.E.M. 邓紫棋.mp3");
- FileInputStream fis = new FileInputStream(file);
- FileOutputStream fos = null;
- //定义一个缓冲字节数组
- byte[] buf = new byte[1024*1024];
- int len = 0;
- int count = 0;//控制知否要创建一个碎片文件存储数据
- int num = 1;
- while ((len=fis.read(buf))!=-1)
- {
- //将count和2取模,这个是控制需要切割的文件的大小。如果和3取模就是每个文件3M,
- //如果需要可以设置传值,这里我就没有弄了
- if (count%2==0)
- {
- fos = new FileOutputStream("F:\\Study\\day20\\02\\"+(num++)+".part");
- fos.write(buf,0,len);
- fos.flush();
- count++;
- }
- else
- {
- fos.write(buf,0,len);
- fos.flush();
- count++;
- }
-
- }
- fos.close();
- fis.close();
- }
复制代码 |
|