文件分割与合并
时间:2022-07-22
本文章向大家介绍文件分割与合并,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
通过RandomAccessFile对象进行文件分割与合并
SplitFile类的初始化
类包含的变量
private String filePath; //源文件路径
private String fileName; //源文件名
private long blockSize; //块大小
private long length; //总长度
private String destPath; //目标路径
private List<String> blockPath; //块路径
private int size; //块数量
构建函数
//均需调用无参构造器,初始化块路径ArrayList
public SplitFile() {
blockPath = new ArrayList<String>();
}
//未指定块大小时默认1024
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");
}
}
- mysql数据与Hadoop之间导入导出之Sqoop实例
- 如何将mysql数据导入Hadoop之Sqoop安装
- 常见的几种Flume日志收集场景实战
- 教你一步搭建Flume分布式日志系统
- 几十条业务线日志系统如何收集处理?
- 0基础搭建Hadoop大数据处理-编程
- 0基础搭建Hadoop大数据处理-集群安装
- Validation of viewstate MAC failed 解决办法
- springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected sin
- springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected sin
- idea启动多个tomcat失败
- Log4Net 生成多个文件、文件名累加解决方法
- 【C#|.NET】lock(this)其实是个坑
- SpringMVC过程中@RequestBody接收Json的问题 总是报415
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- codeforce 266c Below the Diagonal 矩阵变换 (思维题)
- Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)
- 探索ParNew和CMS垃圾回收器
- Codeforce-Ozon Tech Challenge 2020-B. Kuroni and Simple Strings(贪心)
- 第K短路+严格第K短路
- PostgreSQL异常重启postmaster.pid处理
- Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts
- 洛谷P3360偷天换日(树形DP)
- Java开发编程规范: 2.常量定义
- Codeforces Round #509 (Div. 2) A. Heist 贪心
- CodeForces - 1176A Divide it! (模拟+分类处理)
- Codeforces Round #460 (Div. 2)-A Supermaket(贪心)
- Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)
- codeforces 1287A -Angry Students(模拟)
- 洛谷 2016 战略游戏(树形DP)