java实现文件的分割与合并

无非就是io流-------------------------------------------------------

实例类:

1.抽象类

import java.io.File;

import java.io.IOException;

public abstract class PartitionFile {

/**

* 单个文件设置的字节数

*/

public static long MAX_BYTE = 1024*1024*1000;

/**

* 获取可以分割的文件数

*

* @param filePath

* @param max_byte

* @return

*/

public int getPartitionFileNum(long fileByte, String filePath) {

if (MAX_BYTE < fileByte) {

if (fileByte % MAX_BYTE == 0) {

return (int) (fileByte / MAX_BYTE);

} else {

return (int) (fileByte / MAX_BYTE) + 1;

}

}

return 1;

}

/**

* 获取文件长度

*

* @param file

* @return

* @throws IOException

*/

public abstract long getFileLength(File file) throws IOException;

/**

* 分割Byte文件

*

* @param file

* @throws IOException

* @throws IOException

*/

public abstract String[] partitionFile(File outPath,File srcFile, int partitionFileNum) throws IOException;

/**

* 合并文件

* @param files

* @param newFile

* @throws IOException

*/

public abstract void uniteFile(String[] files, String newFile) throws IOException;

}

2.实现类

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

public class SplitBigTextFile extends PartitionFile{

public static void main(String[] args) throws Exception {

/*大文件分割*/

String srcFile = "H:\\proData\\datahktw3\\hk\\b_account_audit.csv";//要分割的文件

String outPath = "E:\\111111111111111111111111111\\";//分割后文件输出位置

File file = new File(srcFile);

File oPath = new File(outPath);

SplitBigTextFile partitionTextFile = new SplitBigTextFile();

int count = partitionTextFile.getPartitionFileNum(file.length(), srcFile);

String str[] = partitionTextFile.partitionFile(oPath,file, count);

System.out.println(str.length);

}

@SuppressWarnings("finally")

@Override

public long getFileLength(File file) throws IOException {

FileReader fr = null;

BufferedReader br = null;

long fileSize = 0;

try {

fr = new FileReader(file);

br = new BufferedReader(fr);

String line = br.readLine();

while (line != null) {

fileSize += line.length();

line = br.readLine();

}

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

} finally {

if (br != null){

br.close();

}

if (fr != null)

fr.close();

return fileSize;

}

}

@Override

public String[] partitionFile(File outPath,File srcFile, int partitionFileNum) throws IOException {

if (partitionFileNum <= 0){

return null;

}

FileReader fr = null;

BufferedReader br = null;

long readNum = 0;

String[] partitions = new String[partitionFileNum];

try {

fr = new FileReader(srcFile);

br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), "gb2312"));

int i = 1;

while (partitionFileNum > i) {

String name = "";

String fileType = "";

if (srcFile.getName().indexOf(".") != -1) {

name = srcFile.getName().substring(0,srcFile.getName().indexOf("."));

fileType = srcFile.getName().substring(srcFile.getName().lastIndexOf(".")+1,srcFile.getName().length());

} else {

name = srcFile.getName();

}

//windows文件分割??

if(outPath.getAbsolutePath().contains("\\")){

partitions[i] = outPath.getParent() + "\\" + name +"\\"+name+"_" + i+".txt";

partitions[i] = partitions[i].toUpperCase();

}else{

//linux文件分割??

partitions[i] = outPath.getParent() + "/" + name +"/"+name+"_" + i+"."+fileType;

}

System.out.println(partitions[i]);

File wfile = new File(partitions[i]);

if (!wfile.exists()) {

wfile.getParentFile().mkdirs();

wfile.createNewFile();

}

FileWriter fw = new FileWriter(wfile,false);

BufferedWriter bw = new BufferedWriter(fw);

String line = br.readLine();

int flush=0;

String []tem = null;

StringBuffer sb = null;

while (line != null) {

sb = new StringBuffer();

if (line.trim().length() == 0) {

line = br.readLine();

continue;

}

tem = line.split("-");

for(int n=0;n<tem.length;n++){

sb.append(tem[n]+",");

}

readNum += sb.toString().length();

if (i + 1 == partitionFileNum) {

bw.write(line);

bw.newLine();

} else {

if (readNum >= MAX_BYTE) {

bw.write(sb.toString());

bw.newLine();

break;

} else {

if(sb.length()>0){

bw.write(sb.toString());

bw.newLine();

}

}

}

line = br.readLine();

if(flush%1000==0){

bw.flush();

}

}

bw.flush();

fw.flush();

bw.close();

fw.close();

if(sb!=null && sb.length()==0){

wfile.delete();

}

readNum = 0;

i++;

}

} finally {

try {

if (br != null){

br.close();

}

if (fr != null){

fr.close();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

br = null;

fr = null;

}

}

return partitions;

}

@Override

public void uniteFile(String[] files, String newFile) throws IOException {

File wFile = new File(newFile);

FileWriter writer = null;

BufferedWriter bufferedWriter = null;

try {

writer = new FileWriter(wFile,false);

bufferedWriter = new BufferedWriter(writer);

for (int i = 0; i < files.length; i++) {

File rFile = new File(files[i]);

FileReader reader = new FileReader(rFile);

BufferedReader bufferedReader = new BufferedReader(reader);

String line = bufferedReader.readLine();

while (line != null) {

if (line.trim().length() == 0) {

line = bufferedReader.readLine();

continue;

}

bufferedWriter.write(line);

bufferedWriter.newLine();

line = bufferedReader.readLine();

}

bufferedWriter.flush();

writer.flush();

}

} finally {

if (bufferedWriter != null){

bufferedWriter.close();

bufferedWriter = null;

}

if (writer != null){

writer.close();

}

writer = null;

}

}

}

时间: 2024-10-10 01:08:23

java实现文件的分割与合并的相关文章

java大文件的分割和合并

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; i

java 流操作对文件的分割和合并的实例详解_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 java 流操作对文件的分割和合并的实例详解 学习文件的输入输出流,自己做一个小的示例,对文件进行分割和合并. 下面是代码: package com.dufy.file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import jav

黑马程序员 IO流 文件的分割与合并

---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ----------------------package cn.itcast.IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;

C#文件的分割与合并

//--------------------文件,分割与合并---------------------------------------- using System.IO /// <summary> /// 单个文件分割函数, /// 可将任意文件fileIn分割为若干个子文件, 单个子文件最大为 len KB /// delet标识文件分割完成后是否删除原文件, change为加密密匙 /// fileIn = "D:\\file.rar", 子文件名形如"D

文件的分割与合并

/*文件的分割*/ #include<stdio.h> #include<stdlib.h> #include<string.h> #define NUM 1024 * 1024 * 100 int main() {         //所需分割的文件路径 FILE *fr = fopen("F:\\PL.exe", "rb"); if (fr == NULL) { exit(-1); } char *buff = (char*)

Python 视频文件的分割和合并

--分割代码 start---- import sys,os; kilobytes = 1024; megabytes = kilobytes*1024; chunksize = int(10*megabytes); def split(fromfile,todir,chunksize=chunksize): if not os.path.exists(todir): os.mkdir(todir) else: for fname in os.listdir(todir): os.remove(

Linux 大文件的分割与合并

1.分割 -- split命令 可以指定按行数分割和按字节大小分割两种模式. (1) 按行数分割 $ split -l 300 large_file.txt new_file_prefix 加上-d,使用数字后缀:加上--verbose,显示分割进度: $ split -l50000 -d large_file.txt part_ --verbose (2) 按字节大小分割 $ split -b 10m large_file.log new_file_prefix 2.合并 -- cat命令 $

对大文件实现分割及合并处理

1 public void cutFile(File bigFile,File destFile,int cutSize){ 2 3 FileInputStream inputStream = null; 4 int size = 1024*1024; //1M 5 try { 6 if (!destFile.isDirectory()){ //如果保存分割文件的地址不是路径 7 destFile.mkdir(); //创建路径 8 } 9 size = size * cutSize; //分割

文件分割与合并(Java)

一.文件分割示意图 二.文件合并示意图 方式一:通过文件追加的方式 方式二:通过SequenceInputStream对其他输入流的逻辑串联. 测试RandomAccessFile随机访问文件 package FileSplitMerge; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import IOOthers.FileUtil; /** * RandomAccessFil