RandomAccessFile
/**
* 文件的分割
* 1. 分割的块数 n块
* 2. 每一块的大小 blocksize
* 3. 最后:总的文件大小 - (n - 1) * blocksize
*/
public void test(){
try {
File file = new File("E:/xp/test/a.txt");
RandomAccessFile rnd = new RandomAccessFile(file,"r");
rnd.seek(10);
byte[] car = new byte[1024];
int len = 0;
while ((len = rnd.read(car)) != -1) {
if (len >= 200) {
System.out.println(new String(car,0,120));
} else {
System.out.println(new String(car,0,len));
}
}
rnd.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
文件分割的思路
- 第一步:分割的准备
- 块数
- 确定每块大小
- 每块的名称
- 第二步
- 分割
- 第几块,每块的起点,实际大小
- 文件分割
public class SplitFile {
// 文件的路径
private String filePath;
// 文件名
private String fileName;
// 文件大小
private long length;
// 块数
private int size;
// 每块的大小
private long blockSize;
// 每块的名称
private List<String> blockPath;
public SplitFile() {
blockPath = new ArrayList<>();
}
public SplitFile(String filePath) {
this(filePath,1024);
}
public SplitFile(String filePath,long blockSize){
this();
this.filePath = filePath;
this.blockSize = blockSize;
init();
}
/**
* 初始化操作,计算块数,确定文件名
*/
public void init() {
File src = null;
if (filePath == null || !((src = new File(filePath)).exists())){
return;
}
if (src.isDirectory()) {
return;
}
// 文件名
this.fileName = src.getName();
// 文件的实际大小
this.length = src.length();
// 修正每块大小
if (this.blockSize > length) {
this.blockSize = length;
}
// 确定块数
size = (int) Math.ceil(length * 1.0 / this.blockSize);
}
/**
* 确定文件名
*/
public void initPathName(String destPath) {
for (int i = 0; i < size; i++){
this.blockPath.add(destPath + "/" + this.fileName + ".part" + i);
}
}
/**
* 文件的分割
* @param destPath 分割文件存放目录
*/
public void split(String destPath){
// 确定文件的路径
initPathName(destPath);
long beginPos = 0;// 起始点
long actualBlockSize = blockSize;//实际大小
// 计算所有块的大小
for (int i = 0; i < size; i++) {
if (i == size - 1){
actualBlockSize = this.length - beginPos;
}
spiltDetail(i,beginPos,actualBlockSize);
beginPos = beginPos + actualBlockSize;
}
}
/**
* 文件的分割 输入 输出
* 文件的拷贝
* @param idx 第几块
* @param beginPos 起始点
* @param actualBlockSize 实际大小
*/
public void spiltDetail(int idx,long beginPos,long actualBlockSize){
// 1. 创建源
File src = new File(this.filePath);
// 2. 目标文件
File dest = new File(this.blockPath.get(idx));
// 3. 选择流
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 = actualBlockSize - len;
} else {
bos.write(flush,0, (int) actualBlockSize);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
合并文件的两种方法
1.
public void mergeFile1(String destPath) {
// 创建源
File dest = new File(destPath);
// 选择流
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(dest,true)); // true: 追加而不是替换
for (int i = 0; i < this.blockPath.size(); i++) {
bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));
// 缓冲区
byte[] flush = new byte[1024];
// 接收长度
int len = 0;
while ((len = bis.read(flush)) != -1) {
bos.write(flush,0,len);
}
bos.flush();
bis.close();
bos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.
public void mergeFile2(String destPath) {
// 创建源
File file = new File(destPath);
// 选择流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
SequenceInputStream sis = null;
// 创建一个容器
Vector<InputStream> vector = new Vector<>();
for (int i = 0; i < this.blockPath.size(); i++){
try {
vector.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
try {
bos = new BufferedOutputStream(new FileOutputStream(file,true));
sis = new SequenceInputStream(vector.elements());
byte[] flush = new byte[1024];
int len = 0;
while ((len = sis.read(flush)) != -1) {
bos.write(flush,0,len);
}
bos.flush();
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}