File Split in Java Program
Java split file into multiple files, this File Split in Java Program code can be used to merge and split files in different sizes depending on your requirement.
Pre-Req:
— Make Sure you have java already installed. Minimum Java version required is 1.4.2.x and above.
To Split A File into Upload Size Slices:
For Splitting:
java Splitme -s <filepath> <Split file size in Mb> Example: java Splitme -s /tmp/temp.zip 1024
Note: Here 1024 is in MB the file splitting size.
For joining:
java Splitme -j <Path To file.sp> Example: java Splitme -j tmp/temp.zip1.sp
Splitme.java – Compile it to class file to execute – Use Javac
/** * TechPaste.com . 2010 * @author Rks(rks2286(at)gmail(dot)com * Modified To Split a File before Upload. * GNU License. The program can be redistributed and modified keeping the author credit intact. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; //import java.io.RandomAccessFile; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class Splitme { // public static long bytefileSize = 0; public static void main(String args[]) { // bytefileSize = Long.parseLong(args[2]); // long mBbytefileSize = bytefileSize * 1024 * 1024; // System.out.println("ByteFileSize" + mBbytefileSize); //Checking the presents of argument. if (args.length == 0) { System.out.println("Type -h for getting help on this splitter"); } else if (args.length == 1) { //if the paramenter is -h, the system displays the syntax for using this utility. if (args[0].equals("-h")) { System.out.println("----------------------------------------------------------------"); System.out.println("For Splitting:"); System.out.println("java Splitme -s <filepath> <Split file size in Mb>"); System.out.println("----------------------------------------------------------------"); System.out.println("For Joining The Splitted File:"); System.out.println("java Splitme -j <Path To file.sp>"); System.out.println("----------------------------------------------------------------"); } // Displaying the error if parameter required are not given. else if (args[0].equals("-s") || args[0].equals("-j")) { System.out.println("Parameters missing, type -h for help....."); } } else if((args.length == 3) && args[2].startsWith("-")) { System.out.println("A Negative(-) Value For The Split File Size Not Allowed."); System.out.println("Type \"Splitme -h\" for Help!"); } //if the parameter is -s then splitting of file happens. else if (args[0].equals("-s")) { if (args.length == 3) { String FilePath = ""; FilePath = args[1]; File filename = new File(FilePath); long splitFileSize = 0,bytefileSize=0; if (filename.exists()) { try { bytefileSize = Long.parseLong(args[2]); // splitFileSize = Long.parseLong(args[2]); splitFileSize = bytefileSize * 1024 * 1024; //System.out.println("ByteFileSize" + mBbytefileSize); Splitme spObj = new Splitme(); spObj.split(FilePath, (long) splitFileSize); spObj = null; } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("File Not Found...."); } filename = null; FilePath = null; } else { System.out.println("Parameters missing, type -h for help....."); } } //if the parameter is -j then joining of file happens. else if (args[0].equals("-j")) { String FilePath = ""; FilePath = args[1]; File filename = new File(FilePath.trim()); if (filename.exists()) { FilePath = FilePath.substring(0, FilePath.length() - 4); filename = new File(FilePath); if (filename.exists()) { System.out.println("\"" + FilePath + "\" File Already Exist...."); } else { Splitme spObj = new Splitme(); spObj.join(FilePath); spObj = null; } } else { System.out.println(FilePath + filename); System.out.println("File Not Found...."); } filename = null; FilePath = null; } System.out.println(); System.out.println(); } public void join(String FilePath) { long leninfile = 0, leng = 0; int count = 1, data = 0; try { File filename = new File(FilePath); //RandomAccessFile outfile = new RandomAccessFile(filename,"rw"); OutputStream outfile = new BufferedOutputStream(new FileOutputStream(filename)); while (true) { filename = new File(FilePath + count + ".sp"); if (filename.exists()) { //RandomAccessFile infile = new RandomAccessFile(filename,"r"); InputStream infile = new BufferedInputStream(new FileInputStream(filename)); data = infile.read(); while (data != -1) { outfile.write(data); data = infile.read(); } leng++; infile.close(); count++; } else { break; } } outfile.close(); } catch (Exception e) { e.printStackTrace(); } } public void split(String FilePath, long splitlen) { long leninfile = 0, leng = 0; int count = 1, data; try { File filename = new File(FilePath); //RandomAccessFile infile = new RandomAccessFile(filename, "r"); InputStream infile = new BufferedInputStream(new FileInputStream(filename)); data = infile.read(); while (data != -1) { filename = new File(FilePath + count + ".sp"); //RandomAccessFile outfile = new RandomAccessFile(filename, "rw"); OutputStream outfile = new BufferedOutputStream(new FileOutputStream(filename)); while (data != -1 && leng < splitlen) { outfile.write(data); leng++; data = infile.read(); } leninfile += leng; leng = 0; outfile.close(); count++; } } catch (Exception e) { e.printStackTrace(); } } }
Sample Outputs:
- Split the large file into 512mb chunks
- Join the split files back into one.
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.
Thanks.. found the needful..
I really like that you are giving information on core and advance java concepts. Being enrolled at course/1779-core-and-advance-java-concepts I found your information very helpful indeed.thanks for it.
I got a question like bwlow so posting it here so that other people will get benefited :
My name is Eddy. I found your java program for split and join files. I splited succsesfull but when I tryied to join the files I had some problems.
The problems is that the pogram only join one sp file
example:
java Splitme -j C:\TrabajoFinal\Destino\Centos.iso.1.sp
the result is a Centos.iso but It only take the first file.
Coud you help me please.
Probable Solution:
It might not be finding all the splitted files in the current path.
cd to C:\TrabajoFinal\Destino\ folder and now try to join with below command
java Splitme -j Centos.iso.1.sp