Log parsing (four) large file segmentation

Source: Internet
Author: User

Split mega files into dozens of m small files, split by size

Package com.fenge;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import java.io.IOException;

public class Fenge {
public static final String SUFFIX = ". txt"; FileName suffix after splitting
Splits the specified file by the number of bytes of the given file, where name refers to the name of the file that needs to be split, and size refers to the specified small file.

public static string[] Divide (String name, long size) throws Exception {
File File = new file (name);
if (!file.exists () | | | (!file.isfile ())) {
throw new Exception ("The specified file does not exist! ");
}
To get the parent file of the split file, the small file will be divided into this directory
File parentfile = File.getparentfile ();
Get the size of the file
Long filelength = File.length ();
SYSTEM.OUT.PRINTLN ("File size:" + filelength + "bytes");
if (size <= 0) {
size = FILELENGTH/2;
}
The number of small files that have been split
int num = (filelength% size! = 0)? (int) (Filelength/size + 1)
: (int) (filelength/size);
Store the small file name after being split
string[] FileNames = new String[num];
Input file stream, which is the split file
FileInputStream in = new FileInputStream (file);
Read the start and end subscript of the input file stream
Long end = 0;
int begin = 0;
Output files based on the number of partitions to be split
for (int i = 0; i < num; i++) {
For the first num–1 small file, the size is specified
File OutFile = new file (Parentfile, file.getname () + i + SUFFIX);
Build a small file output stream
FileOutputStream out = new FileOutputStream (outFile);
End subscript to move size
End + = size;
End = (End > Filelength)? Filelength:end;
Read bytes from the input stream are stored in the output stream
for (; begin < End; begin++) {
Out.write (In.read ());
}
Out.close ();
Filenames[i] = Outfile.getabsolutepath ();
}
In.close ();
return fileNames;
}

public static void Readfilemessage (String fileName) {//read out the contents of the split small file
File File = new file (fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader (file));
string string = null;
Reads the content by line until NULL is read to indicate the end of the read file
while ((string = Reader.readline ()) = null) {
System.out.println (string);
}
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (reader! = null) {
try {
Reader.close ();
} catch (IOException E1) {
}
}
}
}

public static void Main (final string[] args) throws Exception {
String name = "E:/automation/branches/uv/home_fang_com-zhuangxiu2.txt";
Long size = 1024x768 * 1024x768 * 20;//1k=1024b (bytes)
string[] FileNames = fenge.divide (name, size);
System.out.println ("file" + name + "Split result is as follows:");
for (int i = 0; i < filenames.length; i++) {
The contents of System.out.println (Filenames[i] + "are as follows:");
Fengefile.readfilemessage (Filenames[i]);
System.out.println ();
}
}
}

Log parsing (four) large file segmentation

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.