简体   繁体   English

为什么我无法在文件分割程序(Java)中找到正确的解决方案?

[英]Why can't I reach the correct solution in my file splitting program (Java)?

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. 关于代码:基本上我正在编写一个程序,将给定的大文本文件拆分为更小的'n'个大小为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. 计算一行的近似大小(字节),而不是每10mb文件的行数。
  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. 问题:程序正确分成n个文件,但所有文件都有相同的数据。 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. 我希望有人可以给我一个解决方案。

Rather than initiate this 而不是发起这个

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

in the writelines method, writelines方法中,

initiate in the main and pass it as a parameter to filewrite and writelines main启动并将其作为参数传递给filewritewritelines

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM