Java splitting too large text file enhance performance I have a too large data.txt file containing data as follows:

a = line0
line1
line2
€
b = line0
line1
line2
€
...
z = line0
line1


I am still beginner with java, I wrote this code to split the data.txt into multiple .txt files named a.txt , b.txt etc... it splits the data.txt at each €\n character.

It works perfectly, but it is so slow, I noticed, as if it reads everything at once and makes its mission, then at once the result .txt files are created and showed  at once in the destination folder.

How could I make my code faster, for example when it is finished with the first part then it should create the result a.txt file immediatly then the second, third and so on...
Thank in advance

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.*;

public class Main
{
    public static void main(String[] args)
    {
        File file = new File("/path/path2/file.txt");

        try{
            Scanner scanner = new Scanner(file);
            String str = "";


            while(scanner.hasNext()){

                str+=scanner.nextLine()+"\n";

            }


            String charac = "€";
            String end = "end of file";

            for(int i = 0; i &lt; (str.split(charac).length)-1; i++){

                String name = str.split(charac)[i].split(" = ")[0];

                String out= "/path/path2/path3/Folder/"+name+".txt";
                FileWriter fw = new FileWriter(out);
                BufferedWriter bw = new BufferedWriter(fw);

                bw.write(str.split(charac)[i]+"\n"+end);
                bw.close();
            }

            System.out.println("Splitting is finished");

        }
        catch(Exception e){}
    }
}
As said in the comment, you program is slow because you load the full file in memory. To change that you should keep only your first loop where you read but also write and split into files.

while(scanner.hasNext()){
    str+=scanner.nextLine()+"\n";
    if ( mustSplit(str) ) {
        writeToAnotherFile(str); 
        str = "";
    }
}