Java implementation file Segmentation and merging

Source: Internet
Author: User

is the IO stream-------------------------------------------------------

Instance class:

1. Abstract class

Import Java.io.File;
Import java.io.IOException;


Public abstract class Partitionfile {

/**
* Number of bytes in a single file set
*/
public static long max_byte = 1024*1024*1000;


/**
* Get the number of files that can be split
*
* @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;
}

/**
* Get file length
*
* @param file
* @return
* @throws IOException
*/
Public abstract long getfilelength (file file) throws IOException;


/**
* Split byte file
*
* @param file
* @throws IOException
* @throws IOException
*/
Public abstract string[] Partitionfile (File outpath,file srcfile, int partitionfilenum) throws IOException;


/**
* Merge Files
* @param files
* @param newFile
* @throws IOException
*/
public abstract void Unitefile (string[] files, String newFile) throws IOException;

}


2. Implementation class

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 {

/* Large File Split */
String srcfile = "h:\\prodata\\datahktw3\\hk\\b_account_audit.csv";//file to be split
String Outpath = "e:\\111111111111111111111111111\\";//File output location after split
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 file segmentation??
if (Outpath.getabsolutepath (). Contains ("\ \")) {
Partitions[i] = outpath.getparent () + "\ \" + name + "\ \" +name+ "_" + i+ ". txt";

Partitions[i] = Partitions[i].touppercase ();
}else{
Linux file segmentation??
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;
}
}
}


Java implementation file Segmentation and merging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.