简体   繁体   中英

Split file in in n lines and add them to ArrayList of Strings

I want to split a file in lines of 8 and put them into an ArrayList of Strings.

For example, this file:

public class Example {
    private int result = 2 + 1;
    private int result2 = 3 * 2;
    private int result3 = 3 + 3;

    private int result4 = 5 + 1 * 74;
    private String a = "This is a string";
    System.out.println("Just a print");
    int b = 12;

    System.out.println("Another print");
}

Would result in the following two entries ArrayList:

ArrayList<String> fileSplitted = { "public class Example {\nprivate int result = 2 + 1;\nprivate int result2 = 3 * 2;\nprivate int result3 = 3 + 3;\n\nprivate int result4 = 5 + 1 * 74;\nprivate String a = "This is a string";\nSystem.out.println("Just a print");" ; "int b = 12;\n\nSystem.out.println("Another print");\n}" }

So I wrote this function:

public static ArrayList<String> splitFile(File file) throws FileNotFoundException {
        ArrayList<String> fileSplitted = new ArrayList<String>();
        ArrayList<String> fileSplittedReturn = new ArrayList<String>();
        Scanner input = new Scanner(file);
        StringBuilder tmp = new StringBuilder();
        int count = 0;
        int countTotal = 0;

        while (input.hasNextLine()) {
            fileSplitted.add(input.nextLine());
        }

        for (String s : fileSplitted) {
            if (count < 8 && countTotal != fileSplitted.size() - 1) {
                tmp.append(s);
                count++;
            } else if (count == 8 && countTotal != fileSplitted.size() - 1) {
                fileSplittedReturn.add(tmp.toString());
                tmp.setLength(0);
                tmp.append(s);
                count = 1;
            } else if (countTotal == fileSplitted.size() - 1) {
                fileSplittedReturn.add(tmp.toString());
            }
            countTotal++;
        }

        return fileSplittedReturn;
    }

Even though it works (I didn't find any bug yet, but I can't tell for sure), I think that it is kind of heavy and not that easily readable.

Is there a way for me to embellish this function and make it a little more lightweight?

After reading all the line from the file (this is an acceptable approach if we already know that the file won't be too big)

I would group lines using something like

public static List<String> groupLines(List<String> lines, int groupSize){
    int remaining = lines.size() % groupSize;
    int nbSplit = lines.size() / groupSize;

    List<String> target = new ArrayList<>();

    for(int i = 0; i < nbSplit; i++){
        target.add(getLines(lines,i * groupSize, groupSize));
    }

    if(remaining > 0){
        target.add(getLines(lines, nbSplit * groupSize, remaining));
    }

    return target;
}

private static String getLines(List<String> lines, int from, int size){
    return lines.stream()
            .skip(from)
            .limit(size)
            .collect(Collectors.joining("\n"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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