Hey guys:

I am trying to split a file (a video/image) into pieces of 100kb size and then trying to join them back to make the original file. I have a code which splits and joins but I am not getting the same image back. Here's the code, it's pretty long but I present it in entirety. Can anyone please help?

import java.io.*;
import java.util.*;
 
public class StupidTest {
 
	static final int Chunk_Size = 1024*100;
	static int size =0;
 
	public static void main(String[] args) {
		String file = "x.jpg";
		int chunks = DivideFile(file);
		System.out.print((new File(file)).delete());
		System.out.print(JoinFile(file, chunks));
	}
 
	static 	boolean JoinFile(String fname, int nChunks) {
		/*
		 * Joins the chunks together. Chunks have been divided using DivideFile
		 * function so the last part of filename will ".partxxxx" Checks if all
		 * parts are together by matching number of chunks found against
		 * "nChunks", then joins the file otherwise throws an error.
		 */
		boolean successful = false;
		File currentDirectory = new File(System.getProperty("user.dir")); // get
																			// current
																			// directory
		File[] fileList = currentDirectory.listFiles();
		/* populate only the files having extension like "partxxxx" */
		List<File> lst = new ArrayList<File>();
 
		for (File file : fileList) {
			if (file.isFile()) {
				String fnm = file.getName();
				int lastDot = fnm.lastIndexOf('.');
				/*
				 * add to list which match the name given by "fname" and have
				 * "partxxxx" as extension"
				 */
				if (fnm.substring(0, lastDot).equalsIgnoreCase(fname)
						&& (fnm.substring(lastDot + 1)).substring(0, 4).equals("part")) {
					lst.add(file);
				}
			}
		}
 
		/*
		 * sort the list - it will be sorted by extension only because we have
		 * ensured that list only contains those files that have "fname" and
		 * "part"
		 */
		File[] files = (File[]) lst.toArray(new File[0]);
		Arrays.sort(files);
 
 
		/* Ensure that number of chunks match the length of array */
		if (files.length == nChunks) {
			File ofile = new File(fname);
			FileOutputStream fos;
			FileInputStream fis;
			byte[] fileBytes;
			int bytesRead = 0;
			try {
				fos = new FileOutputStream(ofile,true);
 
				for (File file : files) {
					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 (FileNotFoundException fnfe) {
				System.out.println("Could not find file");
				successful = false;
				return successful;
			} catch (IOException ioe) {
				System.out.println("Cannot write to disk");
				successful = false;
				return successful;
			}
			/* ensure size of file matches the size given by server */
			successful = (ofile.length() == StupidTest.size) ? true : false;
		} else {
			successful = false;
		}
		return successful;
	}
 
	static int DivideFile(String fname) {
		File ifile = new File(fname); 
		FileInputStream fis;
		String newName;
		FileOutputStream chunk;
		int fileSize = (int) ifile.length();
		int nChunks = 0, read = 0, readLength = Chunk_Size;
		byte[] byteChunk;
		try {
			fis = new FileInputStream(ifile);
			StupidTest.size = (int)ifile.length();
			while (fileSize > 0) {
				if (fileSize <= Chunk_Size) {
					readLength = fileSize;
				}
				byteChunk = new byte[readLength];
				read = fis.read(byteChunk, 0, readLength);
				fileSize -= read;
				assert(read==byteChunk.length);
				nChunks++;
				newName = fname + ".part" + Integer.toString(nChunks - 1);
				chunk = new FileOutputStream(new File(newName));
				chunk.write(byteChunk);
				chunk.flush();
				chunk.close();
				byteChunk = null;
				chunk = null;
			}
			fis.close();
			fis = null;
		} catch (FileNotFoundException fnfe) {
			System.out.println("Could not find the given file");
			System.exit(-1);
		} catch (IOException ioe) {
			System.out
					.println("Error while creating file chunks. Exiting program");
			System.exit(-1);
		}
		return nChunks;
	}
}