文件分割(每个100k)
package upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
public class split {
public static void main(String[] args) {
int eachSize=100*1024;
File srcFile =new File("D:/Test/Test.txt");//创建一个文件对象
splitFile(srcFile,eachSize);
}
public static void splitFile(File srcFile,int eachSize){
//判断文件是否符合拆分要求
if(srcFile.length()==0){
throw new RuntimeException("文件不符合拆分要求");
}
byte[] fileContent= new byte[(int) srcFile.length()];
try {
//将文件内容读取到内存中
FileInputStream fis=new FileInputStream(srcFile);
fis.read(fileContent);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
//计算要次要拆分为多少份
int fileNumber;
if(fileContent.length%eachSize==0){
fileNumber = fileContent.length/eachSize;
}else{
fileNumber = fileContent.length/eachSize+1;
}
for(int i=0;i<fileNumber;i++){
String fileName = srcFile.getName()+"-"+i+".txt";
File fi = new File(srcFile.getParent(), fileName);//在当前文件路径下创建拆分的文件
byte[] eachContent;
//将源文件内容复制到拆分的文件中
if(i!=fileNumber-1){
eachContent = Arrays.copyOfRange(fileContent, eachSize*i, eachSize*(i+1));
}else{
eachContent = Arrays.copyOfRange(fileContent, eachSize*i, fileContent.length);
}
try {
FileOutputStream fos = new FileOutputStream(fi);
fos.write(eachContent);
fos.close();
System.out.println("输出子文件 "+fi.getAbsoluteFile()+",其大小为"+fi.length()+",每个的大小是"+eachContent.length);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
文件合并
package upload;
import java.io.*;
public class compose {
public static void upload(String destPath,String ... srcPaths){
if(destPath==null||"".equals(destPath)||srcPaths==null){
System.out.println("合并失败");
}
for (String string : srcPaths) {
if("".equals(string)||string==null)
System.out.println("合并失败");
}
//合并后的文件名
String name = srcPaths[0].substring(srcPaths[0].lastIndexOf("\\"));
String destName = name.substring(0, name.lastIndexOf("-"));
destPath = destPath+destName;//合并后的文件路径
File destFile = new File(destPath);//合并后的文件
OutputStream out = null;
BufferedOutputStream bos = null;
try {
out = new FileOutputStream(destFile);
bos = new BufferedOutputStream(out);
for (String src : srcPaths) {
File srcFile = new File(src);
InputStream in = new FileInputStream(srcFile);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[1024*1024];
int len = -1;
while((len = bis.read(bytes))!=-1){
bos.write(bytes, 0, len);
}
bis.close();
in.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
try {
if(bos!=null)bos.close();
if(out!=null)out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String destPath = "D:\\Test";
//要合并的文件路径
String[] srcPaths = {
"D:\\Test\\Test.txt-0.txt",
"D:\\Test\\Test.txt-1.txt",
"D:\\Test\\Test.txt-2.txt",
"D:\\Test\\Test.txt-3.txt",
"D:\\Test\\Test.txt-4.txt",
"D:\\Test\\Test.txt-5.txt",
"D:\\Test\\Test.txt-6.txt",
"D:\\Test\\Test.txt-7.txt"};
System.out.println("开始合并。。。");
upload(destPath, srcPaths);
System.out.println("合并结束");
}
}