通过RandomAccessFile对象进行文件分割与合并
SplitFile类的初始化
类包含的变量
private String filePath;
private String fileName;
private long blockSize;
private long length;
private String destPath;
private List<String> blockPath;
private int size;
构建函数
public SplitFile() {
blockPath = new ArrayList<String>();
}
public SplitFile(String filePath, String destPath) {
this(filePath, 1024, destPath);
}
public SplitFile(String filePath, long blockSize, String destPath) {
this();
this.filePath = filePath;
this.blockSize = blockSize;
this.destPath = destPath;
init();
}
构造器中调用的初始化函数
private void init() {
File src = null;
if (null == filePath || !(((src = new File(filePath)).exists()))) {
return;
}
if (src.isDirectory())
return;
this.fileName = src.getName();
length = src.length();
size = (int) (Math.ceil(length * 1.0 / blockSize));
initPathName();
}
private void initPathName() {
for (int i = 0; i < size; i++) {
this.blockPath.add(destPath + "\\" + i);
}
}
分割文件
public void split() {
long beginPos = 0;
long actualBlockSize = blockSize;
for (int i = 0; i < size; i++) {
if (i == size - 1) {
actualBlockSize = length - beginPos;
}
splitDetail(i, beginPos, actualBlockSize);
beginPos += actualBlockSize;
}
}
分割文件实现细节
private void splitDetail(int i, long beginPos, long actualBlockSize) {
File src = new File(filePath);
File dest = new File(this.blockPath.get(i));
RandomAccessFile raf = null;
BufferedOutputStream bos = null;
try {
raf = new RandomAccessFile(src, "r");
bos = new BufferedOutputStream(new FileOutputStream(dest));
raf.seek(beginPos);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = raf.read(flush))) {
if (actualBlockSize - len >= 0) {
bos.write(flush, 0, len);
actualBlockSize -= len;
} else {
bos.write(flush, 0, (int) actualBlockSize);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
合并文件
采用迭代器以及合并流进行文件合并
public void merge(aString destPath) {
File dest = new File(destPath);
BufferedOutputStream bos = null;
SequenceInputStream sis = null;
Vector<InputStream> vi = new Vector<InputStream>();
try {
for (int i = 0; i < blockPath.size(); i++) {
vi.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
}
bos = new BufferedOutputStream(new FileOutputStream(dest));
sis = new SequenceInputStream(vi.elements());
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = sis.read(flush))) {
bos.write(flush, 0, len);
}
bos.flush();
sis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
采用普通方法合并文件
public void merge1(String destPath) {
File dest = new File(destPath);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(dest));
BufferedInputStream bis = null;
for (int i = 0; i < blockPath.size(); i++) {
bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = bis.read(flush))) {
bos.write(flush, 0, len);
}
bos.flush();
bis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
主函数
public static void main(String[] args) {
SplitFile sp = new SplitFile("D:\\aa\\b\\新建 Microsoft Excel 工作表.xlsx", 50, "d:/aa");
System.out.println(sp.length);
sp.split();
sp.merge("d:/aa/新建 Microsoft Excel 工作表.xlsx");
}
完整代码
package cn.hxh.io.other;
import java.util.*;
import java.io.*;
import java.lang.Math;
public class SplitFile {
private String filePath;
private String fileName;
private long blockSize;
private long length;
private String destPath;
private List<String> blockPath;
private int size;
public SplitFile() {
blockPath = new ArrayList<String>();
}
public SplitFile(String filePath, String destPath) {
this(filePath, 1024, destPath);
}
public SplitFile(String filePath, long blockSize, String destPath) {
this();
this.filePath = filePath;
this.blockSize = blockSize;
this.destPath = destPath;
init();
}
private void init() {
File src = null;
if (null == filePath || !(((src = new File(filePath)).exists()))) {
return;
}
if (src.isDirectory())
return;
this.fileName = src.getName();
length = src.length();
size = (int) (Math.ceil(length * 1.0 / blockSize));
initPathName();
}
private void initPathName() {
for (int i = 0; i < size; i++) {
this.blockPath.add(destPath + "\\" + i);
}
}
public void split() {
long beginPos = 0;
long actualBlockSize = blockSize;
for (int i = 0; i < size; i++) {
if (i == size - 1) {
actualBlockSize = length - beginPos;
}
splitDetail(i, beginPos, actualBlockSize);
beginPos += actualBlockSize;
}
}
private void splitDetail(int i, long beginPos, long actualBlockSize) {
File src = new File(filePath);
File dest = new File(this.blockPath.get(i));
RandomAccessFile raf = null;
BufferedOutputStream bos = null;
try {
raf = new RandomAccessFile(src, "r");
bos = new BufferedOutputStream(new FileOutputStream(dest));
raf.seek(beginPos);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = raf.read(flush))) {
if (actualBlockSize - len >= 0) {
bos.write(flush, 0, len);
actualBlockSize -= len;
} else {
bos.write(flush, 0, (int) actualBlockSize);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void merge(String destPath) {
File dest = new File(destPath);
BufferedOutputStream bos = null;
SequenceInputStream sis = null;
Vector<InputStream> vi = new Vector<InputStream>();
try {
for (int i = 0; i < blockPath.size(); i++) {
vi.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
}
bos = new BufferedOutputStream(new FileOutputStream(dest));
sis = new SequenceInputStream(vi.elements());
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = sis.read(flush))) {
bos.write(flush, 0, len);
}
bos.flush();
sis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void merge1(String destPath) {
File dest = new File(destPath);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(dest));
BufferedInputStream bis = null;
for (int i = 0; i < blockPath.size(); i++) {
bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = bis.read(flush))) {
bos.write(flush, 0, len);
}
bos.flush();
bis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SplitFile sp = new SplitFile("D:\\aa\\b\\新建 Microsoft Excel 工作表.xlsx", 50, "d:/aa");
System.out.println(sp.length);
sp.split();
sp.merge("d:/aa/新建 Microsoft Excel 工作表.xlsx");
}
}