When transferring files through network or uploading to internet, it is common that file size could be the real factor. In those scenarios, splitting the file will solve your problem. The files will be splitted into small parts of chunks, that will be merged into a single file at the destination. This example shows how to achieve that functionality using Java. Here I have used the simple text file for the example and define just “5 bytes” as the part size, you can change the file name and size to split the large files.
Here is the two example program to split and merge the files. When you split the files, it will create multiple small files in your directory. Those file name will be passed as the list to the merge file example.
SplitFileExample.java
package javabeat.net.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Split File Example * * @author Krishna * */ public class SplitFileExample { private static String FILE_NAME = "TextFile.txt"; private static byte PART_SIZE = 5; public static void main(String[] args) { File inputFile = new File(FILE_NAME); FileInputStream inputStream; String newFileName; FileOutputStream filePart; int fileSize = (int) inputFile.length(); int nChunks = 0, read = 0, readLength = PART_SIZE; byte[] byteChunkPart; try { inputStream = new FileInputStream(inputFile); while (fileSize > 0) { if (fileSize <= 5) { readLength = fileSize; } byteChunkPart = new byte[readLength]; read = inputStream.read(byteChunkPart, 0, readLength); fileSize -= read; assert (read == byteChunkPart.length); nChunks++; newFileName = FILE_NAME + ".part" + Integer.toString(nChunks - 1); filePart = new FileOutputStream(new File(newFileName)); filePart.write(byteChunkPart); filePart.flush(); filePart.close(); byteChunkPart = null; filePart = null; } inputStream.close(); } catch (IOException exception) { exception.printStackTrace(); } } }
MergeFileExample.java
package javabeat.net.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; /** * Merge File Example * * @author Krishna * */ public class MergeFileExample { private static String FILE_NAME = "TextFile.txt"; public static void main(String[] args) { File ofile = new File(FILE_NAME); FileOutputStream fos; FileInputStream fis; byte[] fileBytes; int bytesRead = 0; List<File> list = new ArrayList<File>(); list.add(new File(FILE_NAME+".part0")); list.add(new File(FILE_NAME+".part1")); list.add(new File(FILE_NAME+".part2")); list.add(new File(FILE_NAME+".part3")); list.add(new File(FILE_NAME+".part4")); list.add(new File(FILE_NAME+".part5")); list.add(new File(FILE_NAME+".part6")); list.add(new File(FILE_NAME+".part7")); try { fos = new FileOutputStream(ofile,true); for (File file : list) { fis = new FileInputStream(file); fileBytes = new byte[(int) file.length()]; bytesRead = fis.read(fileBytes, 0,(int) file.length()); assert(bytesRead == fileBytes.length); assert(bytesRead == (int) file.length()); fos.write(fileBytes); fos.flush(); fileBytes = null; fis.close(); fis = null; } fos.close(); fos = null; }catch (Exception exception){ exception.printStackTrace(); } } }