- importjava.io.BufferedReader;
-
importjava.io.BufferedWriter;
-
importjava.io.File;
-
importjava.io.FileInputStream;
-
importjava.io.FileOutputStream;
-
importjava.io.IOException;
-
importjava.io.InputStream;
-
importjava.io.InputStreamReader;
-
importjava.io.OutputStream;
-
importjava.io.OutputStreamWriter;
-
-
-
-
publicclassCutJoinFile{
-
-
privatestaticintMAX_PART=100;
-
-
publicstaticvoidmain(String[]args)throwsIOException{
-
Stringfilname="d:/cvs.rar";
-
cutFileBin(filname,2*1024*1024);
-
joinFileBin(filname);
- }
-
-
-
-
publicstaticvoidcutFileText(StringfileName,intsize)throwsIOException{
-
BufferedReaderreader=newBufferedReader(newInputStreamReader(newFileInputStream(fileName)));
-
intindex=fileName.lastIndexOf(".");
-
Stringext="";
-
if(index>0){
- ext=fileName.substring(index);
-
fileName=fileName.substring(0,index);
- }
-
intlen;
-
intcount=1;
-
char[]cbuf=newchar[size];
-
while((len=reader.read(cbuf,0,size))!=-1){
-
BufferedWriterwriter=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(fileName+"_"+count+ext)));
-
writer.write(cbuf,0,len);
- writer.flush();
- writer.close();
- count++;
-
if(count>MAX_PART){
-
break;
- }
- }
- }
-
-
-
-
publicstaticvoidcutFileBin(StringfileName,intsize)throwsIOException{
-
InputStreamis=newFileInputStream(fileName);
-
intindex=fileName.lastIndexOf(".");
-
Stringext="";
-
if(index>0){
- ext=fileName.substring(index);
-
fileName=fileName.substring(0,index);
- }
-
intlen;
-
intcount=1;
-
byte[]cbuf=newbyte[size];
-
while((len=is.read(cbuf,0,size))!=-1){
-
OutputStreamos=newFileOutputStream(fileName+"_"+count+ext);
-
os.write(cbuf,0,len);
- os.close();
- count++;
-
if(count>MAX_PART){
-
break;
- }
- }
- }
-
-
-
-
publicstaticvoidjoinFileText(StringfileName)throwsIOException{
-
intindex=fileName.lastIndexOf(".");
-
Stringext="";
-
if(index>0){
- ext=fileName.substring(index);
-
fileName=fileName.substring(0,index);
- }
-
-
BufferedWriterwriter=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(fileName+"_join"+ext)));
-
intlen;
-
char[]cbuf=newchar[1024*100];
- Filefile;
-
for(inti=1;i<=Integer.MAX_VALUE;i++){
-
file=newFile(fileName+"_"+i+ext);
-
if(!file.exists()){
-
break;
- }
-
BufferedReaderreader=newBufferedReader(newInputStreamReader(newFileInputStream(file)));
-
while((len=reader.read(cbuf,0,cbuf.length))!=-1){
-
writer.write(cbuf,0,len);
- writer.flush();
- }
- reader.close();
- }
- writer.close();
- }
-
-
-
-
publicstaticvoidjoinFileBin(StringfileName)throwsIOException{
-
intindex=fileName.lastIndexOf(".");
-
Stringext="";
-
if(index>0){
- ext=fileName.substring(index);
-
fileName=fileName.substring(0,index);
- }
-
OutputStreamwriter=newFileOutputStream(fileName+"_join"+ext);
-
intlen;
-
byte[]cbuf=newbyte[1024*100];
- Filefile;
-
for(inti=1;i<=Integer.MAX_VALUE;i++){
-
file=newFile(fileName+"_"+i+ext);
-
if(!file.exists()){
-
break;
- }
-
InputStreamreader=newFileInputStream(file);
-
while((len=reader.read(cbuf,0,cbuf.length))!=-1){
-
writer.write(cbuf,0,len);
- writer.flush();
- }
- reader.close();
- }
- writer.close();
- }
- }
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 对文件进行分割与合并的工具
*
* @author Administrator
*/
public class CutJoinFile {
private static int MAX_PART = 100; // 最大分割100分,以免手误引起麻烦
public static void main(String[] args) throws IOException {
String filname = "d:/cvs.rar";
cutFileBin(filname, 2 * 1024 * 1024); // 二进制文件为例
joinFileBin(filname); // 二进制文件为例
}
/**
* 分割文本文件
*
* @param fileName 文件名
* @param size 每个分割小文件的尺寸
* @throws IOException
*/
public static void cutFileText(String fileName, int size) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
int index = fileName.lastIndexOf(".");
String ext = "";
if (index > 0) {
ext = fileName.substring(index);
fileName = fileName.substring(0, index);
}
int len;
int count = 1;
char[] cbuf = new char[size];
while ((len = reader.read(cbuf, 0, size)) != -1) {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_" + count + ext)));
writer.write(cbuf, 0, len);
writer.flush();
writer.close();
count++;
if (count > MAX_PART) {
break;
}
}
}
/**
* 分割二进制文件
*
* @param fileName 文件名
* @param size 每个分割小文件的尺寸
* @throws IOException
*/
public static void cutFileBin(String fileName, int size) throws IOException {
InputStream is = new FileInputStream(fileName);
int index = fileName.lastIndexOf(".");
String ext = "";
if (index > 0) {
ext = fileName.substring(index);
fileName = fileName.substring(0, index);
}
int len;
int count = 1;
byte[] cbuf = new byte[size];
while ((len = is.read(cbuf, 0, size)) != -1) {
OutputStream os = new FileOutputStream(fileName + "_" + count + ext);
os.write(cbuf, 0, len);
os.close();
count++;
if (count > MAX_PART) {
break;
}
}
}
/**
* 合并文本文件
*
* @param fileName 文件名,不包括编号部分
* @throws IOException
*/
public static void joinFileText(String fileName) throws IOException {
int index = fileName.lastIndexOf(".");
String ext = "";
if (index > 0) {
ext = fileName.substring(index);
fileName = fileName.substring(0, index);
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_join" + ext)));
int len;
char[] cbuf = new char[1024 * 100];
File file;
for (int i = 1; i <= Integer.MAX_VALUE; i++) {
file = new File(fileName + "_" + i + ext);
if (!file.exists()) {
break;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {
writer.write(cbuf, 0, len);
writer.flush();
}
reader.close();
}
writer.close();
}
/**
* 合并二进制文件
*
* @param fileName 文件名,不包括编号部分
* @throws IOException
*/
public static void joinFileBin(String fileName) throws IOException {
int index = fileName.lastIndexOf(".");
String ext = "";
if (index > 0) {
ext = fileName.substring(index);
fileName = fileName.substring(0, index);
}
OutputStream writer = new FileOutputStream(fileName + "_join" + ext);
int len;
byte[] cbuf = new byte[1024 * 100];
File file;
for (int i = 1; i <= Integer.MAX_VALUE; i++) {
file = new File(fileName + "_" + i + ext);
if (!file.exists()) {
break;
}
InputStream reader = new FileInputStream(file);
while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {
writer.write(cbuf, 0, len);
writer.flush();
}
reader.close();
}
writer.close();
}
}
分享到:
相关推荐
java实现大文件分割与合并的实例代码,需要的朋友可以参考一下
文件切割和文件合并这个问题困扰了我有一段时间了(超过一天没做粗来)。 找了好多博客,本来想转载一个来的 结果找不到了。很无奈。 只好自己贴代码上了。 当然我会尽力好好写注释的。 文件切割器: import java.io...
主要介绍了Java实现多个wav文件合成一个的方法,涉及java文件流读写、编码转换、解析等相关操作技巧,需要的朋友可以参考下
主要介绍了Java实现文件分割与合并,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
主要介绍了Java实现大文件的切割与合并操作,结合实例形式分析了java基于io及util操作大文件按指定个数分割与合并相关操作技巧,需要的朋友可以参考下
本篇文章主要介绍了Java将文件分割为多个子文件再将子文件合并成原始文件的示例,具有一定的参考价值,有兴趣的可以了解一下。
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
主要介绍了Java 使用IO流实现大文件的分割与合并实例详解的相关资料,需要的朋友可以参考下
主要介绍了java 流操作对文件的分割和合并的实例详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
基于Java的实例源码-PDF分割与合并源代码.zip
基于Java的实例开发源码-PDF分割与合并源代码.zip
实例127 一个文件变成多个小文件 178 实例128 多个小文件合成一个文件 181 实例129 统计指定文件中的字符个数 183 实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成...
PDF分割与合并源代码.rar
数据压缩,文件传输 Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧...