skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 27, 2012

SPLIT HUGE FILES INTO SMALL TEXT FILES

Posted by Raju Gupta at 7:00 AM – 0 comments
 

This code snippet is used for automatic splitting of files conatining large content into smaller text files with specific number of records in each output file. The number of output files depends upon the number of lines to be splitted and the number of lines in the original file.

import java.io.*;
import java.util.Scanner;
public class split {
public static void main(String args[])
{
 try{
  // Reading file and getting no. of files to be generated
  String inputfile = "C:/test.txt"; //  Source File Name.
  double nol = 2000.0; //  No. of lines to be split and saved in each output file.
  File file = new File(inputfile);
  Scanner scanner = new Scanner(file);
  int count = 0;
  while (scanner.hasNextLine()) 
  {
   scanner.nextLine();
   count++;
  }
  System.out.println("Lines in the file: " + count);     // Displays no. of lines in the input file.

  double temp = (count/nol);
  int temp1=(int)temp;
  int nof=0;
  if(temp1==temp)
  {
   nof=temp1;
  }
  else
  {
   nof=temp1+1;
  }
  System.out.println("No. of files to be generated :"+nof); // Displays no. of files to be generated.
 
  //---------------------------------------------------------------------------------------------------------

  // Actual splitting of file into smaller files

  FileInputStream fstream = new FileInputStream(inputfile); DataInputStream in = new DataInputStream(fstream);

  BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;
 
  for (int j=1;j<=nof;j++)
  {
   FileWriter fstream1 = new FileWriter("C:/New Folder/File"+j+".txt");     // Destination File Location
   BufferedWriter out = new BufferedWriter(fstream1); 
   for (int i=1;i<=nol;i++)
   {
    strLine = br.readLine(); 
    if (strLine!= null)
    {
     out.write(strLine); 
     if(i!=nol)
     {
      out.newLine();
     }
    }
   }
   out.close();
  }

  in.close();
 }catch (Exception e)
 {
  System.err.println("Error: " + e.getMessage());
 }

}

} 


Labels: Java File Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger