Java splitting too large text file enhance performance
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 = "";
    }
}