Krokodeeal Krokodeeal - 2 months ago 21
Java Question

Change number of split

Today, my code works but it makes a split of 2 lines by 2 lines, but that's not the goal.
I want to change the split, i want to divide 50 lines by 50 lines and not 2 by 2.
If I leave like that, I have three files of 2 lines
If i have 135 lines, i want:


  • Fist file: 50 lines (line 1-50 of my file) and the file name is 801.

  • Second file: 50 lines (line 51-100 of my file) and the file name is 802.

  • Third file: 35 lines (line 101-135 of my file) and the file name is 803.



Here, it is my code:

private void MyCode(ProducerTemplate producerTemplate, InputStream content, String endpoint, String fileName, HashMap<String, Object> headers){
ArrayList<String> list = new ArrayList<String>();
String line;
long numSplits = 50; //Number of lines per split
int sourcesize=0; //Number of lines
int number = 800; //my filename
BufferedReader br = new BufferedReader(new InputStreamReader(content));

try{

while((line = br.readLine()) != null){
sourcesize++;
list.add(line); //add the lines in the arraylist
}

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

for( int i = 0 ; i <= list.size() ; i+=numSplits) {
String Filename = ""+ number;
System.out.println(Filename);
StringBuilder builder = new StringBuilder();
builder.append(list.get(i)).append(System.lineSeparator()).append(list.get(i+1));
producerTemplate.sendBodyAndHeader(endpoint, builder.toString(), "CamelFileName",Filename); //Used to forward lines and the file name
}

} catch (IOException e) {
e.printStackTrace();
}
}


I think about:

for (int k=0;k<numSplits;k++) {
builder.append(list.get(i));
builder.append(System.lineSeparator());
}


How to change the split?
How can I cut my
arraylist
all 50 occurrences?

Answer
private void MyCode(ProducerTemplate producerTemplate, InputStream content, String endpoint, String fileName, HashMap<String, Object> headers){
    BufferedReader br = new BufferedReader(new InputStreamReader(content));

    ArrayList<String> list = new ArrayList<String>();
    String line;
    int numSplits = 50; 
    int number = 800;          
    int sourcesize=0; 
    try{
        while((line = br.readLine()) != null){
            sourcesize++;
            list.add(line); 
        }
    }
    catch (IOException e){
        e.printStackTrace();
    }
    System.out.println("Lines in the file: " + sourcesize);
    ArrayList<ArrayList<String>> parts = cutListInChunksOfSameSize(list, numSplits);

    for (ArrayList<String> part : parts){
         String  Filename = ""+ (number++);
         System.out.println(Filename);
         StringBuilder builder = new StringBuilder();

         for( int i = 0 ; i <part.size() ;i++){   
             builder.append(part.get(i)).append(System.lineSeparator());                
        }
        producerTemplate.sendBodyAndHeader(endpoint, builder.toString(), "CamelFileName",Filename); 
    }
}

public <T> ArrayList<ArrayList<T>> cutListInChunksOfSameSize(ArrayList<T> list, final int L) {
    ArrayList<ArrayList<T>> parts = new ArrayList<ArrayList<T>>();
    final int N = list.size();
    for (int i = 0; i < N; i += L) {
      parts.add(new ArrayList<T>(list.subList(i, Math.min(N, i + L))));
    }
    return parts;
}
Comments