0

I am reading a Simple Notepad Text file containing a lot of data actually in a 3mb of size so you can imagine the number of words it can have! The problem is I am reading this file into a string then splits the string so that I can hold each single word inside an ArrayList(String). It works fine for me but the actual problem is that I am processing this array list for some purpose and then again I have to append or you can say put all the words of array list back to the String!


so that the steps are:

  1. I read a text file into a String (alltext)
  2. Split all words into an arraylist
  3. process that array list (suppose I removed all the stop words like is, am, are)
  4. after processing on array list I want to put all the words of array list back to the string (alltext)
  5. then I have to work with that string (alltext) (alltext is the string that must contains the text after all processing)

The problem is that at step number 4 it takes a lot of time to append all the words back to the string my code is:


BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
   alltext += line.trim().replaceAll("\\s+", " ") + " ";
}
br.close();

    //Adding All elements from all text to temp list            
    ArrayList<String> tempList = new ArrayList<String>();
    String[] array = alltext.split(" ");
    for (String a : array) {
        tempList.add(a);
    }

    //remove stop words here from the temp list

    //Adding File Words from List  in One String 
    alltext = "";

    for (String removed1 : tempList) {
        System.out.println("appending the text");
        alltext += removed1.toLowerCase() + " ";
        //here it is taking a lot of time suppose 5-10 minutes for a simple text file of even 1.4mb
    }

So I just want any idea so that I can reduce the time for an efficient processing and relax the machine! I will be thankful for any suggestions and ideas... Thanks

Java Nerd
  • 958
  • 3
  • 19
  • 51

2 Answers2

2

Use a StringBuffer instead of a String.

A String is immutable and thus you create a new Object everytime you append, which takes more and more time the longer your String becomes. A StringBuffer is mutable and made for cases like yours.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • The previous time it was taking=5min and 32 seconds and after using stringbuffer or string builder the time reduced to 1 minute and 2 seconds thanks for the idea! – Java Nerd Apr 10 '14 at 13:17
1

I would recommend StringBuilder According to this stringbuilder-and-stringbuffer-in-java it's faster than a StringBuffer also check if you need the ArrayList because you can iterate through the array too

Community
  • 1
  • 1
0riginal
  • 137
  • 1
  • 11