I am trying to divide a binary file (like video/audio/image) into chunks of 100kb each and then join those chunks back to get back the original file. My code seems to be wor
Are there more than 10 chunks? Then the program will concatenate *.part1 + *.part10 + *.part2 and so on.
I can spot only 2 potential mistakes in the code:
int fileSize = (int) ifile.length();
The above fails when the file is over 2GB since an int
cannot hold more.
newName = fname + ".part" + Integer.toString(nChunks - 1);
A filename which is constructed like that should be sorted on a very specific manner. When using default string sorting, name.part10
will namely come before name.part2
. You'd like to supply a custom Comparator which extracts and parses the part number as an int and then compare by that instead.
It takes split file name & destination file size(in byte) form user and split it into subfiles its working for all type of files like(.bin,.jpg,.rar)
import java.io.*;
class split{
public static void main(String args[])throws IOException {
String a;
int b;
long len;
Console con=System.console();
System.out.println("Enter File Name: ");
File f=new File(con.readLine());
System.out.println("Enter Destination File Size: ");
b=Integer.parseInt(con.readLine());
FileInputStream fis=new FileInputStream(f);
len=f.length();
int c=(int)len/b;
if(((int)len%b)!=0)
c++;
for(int i=0;i<c;i++){
File f1=new File(i+""+"."+f);
FileOutputStream fos=new FileOutputStream(f1);
for(int j=0;j<b;j++){
int ch;
if((ch=fis.read())!=-1)
fos.write(ch); } }
fis.close();
System.out.println("Operation Successful"); }}
and another program will merge all the split files.It take only split file name and merge all the files.
import java.io.*;
class merge{
static int i;
public static void main(String args[])throws IOException{
String a;
int b;
long len;
Console con=System.console();
System.out.println("Enter File to be retrived: ");
File f=new File(con.readLine());
FileOutputStream fos=new FileOutputStream(f,true);
try {
File f1=new File(i+""+"."+f);
while((f1.exists())!=false) {
int ch;
FileInputStream fis=new FileInputStream(i+""+"."+f);
i++;
while((ch=fis.read())!=-1){
fos.write(ch); }}}
catch(FileNotFoundException e1){} }}
What happens when you do a binary comparison of the files. e.g. with diff. Do you see a difference after the first file?
Can you try breaking up a text TXT file? if there are bytes are out of place it should be more obvious what is going wrong. e.g. a repeated block/file/or data full of nul bytes. ??
EDIT: As others have noticed, you read the files in no particular order. What you can do is use a padded file number like.
newName = String.format("%s.part%09d", fname, nChunks - 1);
This will give you up to 1 billion files in numeric order.
When you read the files, you need to ensure they are sorted.
Arrays.sort(files);
for (File file : files) {
Using a custom comparator as others have suggest would reduce the size of the padded numbers but it can be nice to be able to sort by name to get the correct order. e.g. in explorer.
And for joining file, I put the names of all chunks in a List, then sort it by name and then run the following code:
But your names are of the following form:
newName = fname + ".part" + Integer.toString(nChunks - 1);
Think carefully about what happens if you have 11 or more parts. Which string comes first in alphabetical order: ".part10" or ".part2"? (Answer: ".part10", since '1' comes before '2' in the character encoding.)
For splitting the file:----->
import java.io.*;
class Split
{
public static void main(String args[])throws IOException
{
Console con=System.console();
System.out.println("enter the file name");
String path=con.readLine();
File f= new File(path);
int filesize=(int)f.length();
FileInputStream fis= new FileInputStream(path);
int size;
System.out.println("enter file size for split");
size=Integer.parseInt(con.readLine());
byte b[]=new byte[size];
int ch,c=0;
while(filesize>0)
{
ch=fis.read(b,0,size);
filesize = filesize-ch;
String fname=c+"."+f.getName()+"";
c++;
FileOutputStream fos= new FileOutputStream(new File(fname));
fos.write(b,0,ch);
fos.flush();
fos.close();
}
fis.close();
}
}