Split file into multiple files

2020-05-09 10:02发布

问题:

I want to cut a text file. I want to cut the file 50 lines by 50 lines.

For example, If the file is 1010 lines, I would recover 21 files.

I know how to count the number of files, the number of lines but as soon as I write, it's doesn't work.

I use the Camel Simple (Talend) but it's Java code.

private void ExtractOrderFromBAC02(ProducerTemplate producerTemplate, InputStream content, String endpoint, String fileName, HashMap<String, Object> headers){
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new InputStreamReader(content));
        String line;
        long numSplits = 50;                
        int sourcesize=0;
        int nof=0;
        int number = 800;
        try {               
            while((line = br.readLine()) != null){
                    sourcesize++;
                    list.add(line);
            }

         System.out.println("Lines in the file: " + sourcesize);    

        double numberFiles = (sourcesize/numSplits);  
        int numberFiles1=(int)numberFiles;  
                if(sourcesize<=50)   {  
                  nof=1;  
                }  
                else  {  
                     nof=numberFiles1+1;  
                }  
       System.out.println("No. of files to be generated :"+nof);

       for (int j=1;j<=nof;j++) {  
                 number++;
                 String  Filename = ""+ number;
                 System.out.println(Filename);

            StringBuilder builder = new StringBuilder();
            for (String value : list) {
                builder.append("/n"+value);
            }

             producerTemplate.sendBodyAndHeader(endpoint, builder.toString(), "CamelFileName",Filename);
        }

             }  

         } catch (IOException e) {
                e.printStackTrace();
         }
            finally{
                try {
                    if(br != null)br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

For people who don't know Camel, this line is used to send the file:

producerTemplate.sendBodyAndHeader (endpoint, line.toString (), "CamelFileName" Filename);

endpoint ==> Destination (it's ok with another code)

line.toString () ==> Values

And then the file name (it's ok with another code)

回答1:

you count the lines first

while((line = br.readLine()) != null){
                    sourcesize++; }

and then you're at the end of the file: you read nothing

for (int i=1;i<=numSplits;i++)  {  
                while((line = br.readLine()) != null){

You have to seek back to the start of the file before reading again.

But that's a waste of time & power because you'll read the file twice

It's better to read the file once and for all, put it in a List<String> (resizable), and proceed with your split using the lines stored in memory.

EDIT: seems that you followed my advice and stumbled on the next issue. You should have maybe asked another question, well... this creates a buffer with all the lines.

for (String value : list) {
                builder.append("/n"+value);
            }

You have to use indexes on the list to build small files.

for (int k=0;k<numSplits;k++) {  
      builder.append("/n"+list[current_line++]);

current_line being the global line counter in your file. That way you create files of 50 different lines each time :)