Showing posts with label FileHandling. Show all posts
Showing posts with label FileHandling. Show all posts

Thursday, August 9, 2012

FileSpliter in JAVA


Divide a file into multiple parts

import java.util.*;
import java.io.*;

class Spliter
{
public static void main(String args[]) throws Exception
{
 long size;
 
 System.out.println("Enter the file name You want to spilit");
 
 Console con=System.console();
 String str=con.readLine();
 
 File f=new File(str);
 
 
  if(f.exists())
  {
  size=f.length();
  System.out.println(size+" byte");
 
  con.printf("Enter Destination FIle Size :-");
  double l=Integer.parseInt(con.readLine());
 
  int num=(int)(Math.ceil(size/l));
  System.out.println("Number of files = "+(num));
  FileInputStream fis=new FileInputStream(f);
 
 
  int ch,j=1;
  String arr[]=new String[num]; 
  for(int i=0;i<num;i++,j++)
  {
         arr[i]=j+f.getName();
         File f1=new File(arr[i]);
                  FileOutputStream fos=new FileOutputStream(f1);
          
         int k=1;
                                    label1:           
         while((ch=fis.read())!=-1)
         {
          if(k<l)
          {
              fos.write(ch);
                        k++;
                  }
                  else if(k>=l)
                  {
                  fos.write(ch);
                break label1;
                  }
             }
     fos.close();
  }
  fis.close();
 
  }
}
}

Steps :-                                                  Compile


RUN


OutPut






    ShareThis