62,617
社区成员
发帖
与我相关
我的任务
分享
public class FileDivision {
// 单个文件大小
private static final long SPLIT_SIZE = 5 * 1024 * 1024;
/**
*分割文件
*
* @author Jason,Wang
* @param filePath
* 要分割的文件路径
* @return int 分割的数量
*/
public static int split(String filePath) {
// 子文件数量
int number = 1;
// 文件通道
FileChannel in = null;
try {
in = new FileInputStream(filePath).getChannel();
// 文件长度
long fileLength = in.size();
long position = 0;
while (position < fileLength) {
// 子文件
FileChannel out = new FileOutputStream(filePath + (number++))
.getChannel();
in.transferTo(position, SPLIT_SIZE, out);
out.close();
position += SPLIT_SIZE;
}
} catch (Exception e) {
e.printStackTrace();
number = 1;
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return number;
}
}