Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

spliiting code in java-don't know what's wrong

$
0
0
hi every one, i'm writing a code to split a file into many files with a size specified in the code, and then it will join these parts again.
the problem is with the joining code,it doesn't work and i can't figure what is wrong, can any one help?
this is my code.

import java.io.*;
import java.util.*;
 
public class StupidSplit {
 static final int Chunk_Size = 10;
	
	static int size =0;
 
	public static void main(String[] args) throws IOException {
		String file = "b.txt";
		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>();
             //  
               Arrays.sort(fileList);
		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);
 System.out.println("size ="+files.length);
System.out.println("hello");
		/* Ensure that number of chunks match the length of array */
		if (files.length == nChunks-1) {
                   
			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() == StupidSplit.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();
                double fileSize = (double) ifile.length();
		//int nChunks = 0, read = 0, readLength = Chunk_Size;
                int nChunks = 0, read = 0, readLength = Chunk_Size;
		byte[] byteChunk;
		try {
			fis = new FileInputStream(ifile);
			StupidSplit.size = (int)ifile.length();
			while (fileSize > 0) {
				if (fileSize <= Chunk_Size) {
					readLength =  (int) 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);
                                newName = String.format("%s.part%09d", fname, nChunks - 1);
				chunk = new FileOutputStream(new File(newName));
				chunk.write(byteChunk);
				chunk.flush();
				chunk.close();
				byteChunk = null;
				chunk = null;
			}
			fis.close(); 
                        System.out.println(nChunks);
		//	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);
		}System.out.println(nChunks);  
		return nChunks;
               
           
	}   
   
}
        }
        
        
        


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>