I see that you already have a working solution based on Kevin's comment, but it may be useful to understand why your old code was slow.
The problem is that when concatenating strings directly using the + operator or using the concat() method, it actually creates a new string with the result of concatenation. This includes copying the entire contents of both lines that you combine. So, when you read in a file and create a content line, every time you add to it, you create another copy of everything that you still have in the content.
The solution, which I believe is what you are doing now, is to use the StringBuffer class, which is a dynamic buffer that allows you to change its contents (i.e. add new data to the end), without having to select a new String object and copy everything to it.
The main thing you should keep in mind is that strings in java are immutable. Each time you call a method in a java string that somehow changes it, a new copy of the string is actually created with the result of the modification.
source share