1

About the code: Basically I'm writing a program that would split a given huge text file into smaller 'n' number of files of size 10 MB.

Workflow of the code:

  1. Opens source file, find number of lines present in file.
  2. Calculate approximate size(bytes) for a line and no of lines per 10mb file.
  3. Find no.of files needed to create and create them.
  4. Then copy paste from source files into split files.

Problem: The program correctly splits into n number of files, but all files have same data. This is because after my program completes the first split file,for the second file the reader automatically goes back to first line in the original source file. So all split files contain the same data as that of the first split file.

Code:

package filespliter;
import java.io.*;
public class FileSpliter {
    public int filecount(FileReader f) throws IOException
    {
      BufferedReader buf=new BufferedReader(f);
      int count=0;
      while(buf.readLine()!=null)
       {
        count++; 
       }
      buf.close();
    return count;
    }

    public int split_value_generator(File f, int count)
    {
      double b=(f.length());
      System.out.println((b/1024)+"kb");
      double i=(b/count);
      System.out.println(" No. of bytes per line :"+i);
      int splitfactor = ((int)(((1024*1024)*10)/i));
      System.out.println(splitfactor+" lines per file");
      return splitfactor;
    }

    public static void filecreate(int index)
    {
        String no;
        int i=1;
        while(index!=0) //index will be number of files I need to create
        {            
        no=Integer.toString(i);
        String key = ("Split"+no);
        key=("e:\\wipro\\splits\\Split"+no+".txt");
        File file= new File(key);
        index--;
        i++;
        }
       i=1; 
    }

    public static void filewrite(int nof,int nol_pf) throws IOException
    { //nof - no. of files   ||  nol_pf - no. of lines per file
        BufferedWriter bwrite;
        String key;       
        int index=1; 
        while(index<=nof)
        {

            key="e:\\wipro\\splits\\Split"+index+".txt";
            System.out.println(key);
            bwrite=new BufferedWriter(new FileWriter(key));
            writelines(bwrite,nol_pf);
            index++;
            iteration+=nolpf2;
        }
            //bwrite.flush();
           // bwrite.close();
          System.out.println("Finished !");  
    }

    public static void writelines(BufferedWriter b, int nol_pf)throws IOException
    {
            String temp;
            BufferedReader scan=new BufferedReader(new FileReader("E://wipro//CBDL.txt"));
            while(nol_pf!=0)
              {
                  temp=scan.readLine();
                    b.write(temp);
                    b.newLine();
                    nol_pf--;

                }          
        }


    public static void main(String[] args) throws FileNotFoundException, IOException {
        String path="E://wipro//CBDL.txt";
        File source=new File(path);
        FileReader file=new FileReader(source);
        FileSpliter obj=new FileSpliter();
        int count=obj.filecount(file);
        int no_of_lines_per_file = obj.split_value_generator(source, count);
        int no_of_files=count/no_of_lines_per_file;
        System.out.println("Need to create "+no_of_files+" files ");
        filecreate(no_of_files);
        file.close();
        filewrite(no_of_files,no_of_lines_per_file);


    }

}

Output: The logic works, n number of smaller files are created, but the all the split files contain the data of the first split file.

Thank you for reading up till now. I hope some one could give me a solution.

1 Answers1

0

Rather than initiate this

BufferedReader scan=new BufferedReader(new FileReader("E://wipro//CBDL.txt"));

in the writelines method,

initiate in the main and pass it as a parameter to filewrite and writelines

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64