package wenjianyuliu;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件
public class CutFile {
public static void main(String[] args) {
//调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
cutFile("E:\\新建文件夹\\poem2.txt", "E:\\80747",1024 * 1024 * 20
);
}
private static void cutFile(String src, String endsrc,
int num) {
FileInputStream fis =
null;
File file =
null;
try {
fis =
new FileInputStream(src);
file =
new File(src);
//创建规定大小的byte数组
byte[] b =
new byte[num];
int len = 0
;
//name为以后的小文件命名做准备
int name = 1
;
//遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
while ((len = fis.read(b)) != -1
) {
//分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
String name2 =
file.getName();
int lastIndexOf = name2.lastIndexOf("."
);
String substring = name2.substring(0
, lastIndexOf);
String substring2 =
name2.substring(lastIndexOf, name2.length());
FileOutputStream fos =
new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name +
substring2);
//将byte数组写入对应的小文件中
fos.write(b, 0
, len);
//结束资源
fos.close();
name++
;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis !=
null) {
//结束资源
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转载于:https://www.cnblogs.com/zzstdruan1707-4/p/9979707.html
相关资源:Java大文件传输示例额