例:将一个MP3的文件以200kb拆分为多个文件
代码如下:
package work2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class SplitDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(
new FileInputStream("C:\\Users\\Administrator\\Desktop\\mp3.m4a"));
byte[] buf = new byte[1024 * 200];// 200kb
int len;
int count = 1;
while ((len = bis.read(buf)) != -1) {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
"C:\\Users\\Administrator\\Desktop\\mp3.m4a" + count));
bos.write(buf, 0, len);
bos.flush();
bos.close();
count++;
}
System.out.println("拆分成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
注:主页更多学习资料。