Hello All,
Can everyone help me to solve this problem ?
Im doing an assignment about downloading a file and write it to a new file.
My problem is the file that I was trying to download is very large, so everytime I compile my Download class and the main program, it taking very long time. To speed it up, I been tryin to modify this Download class so that it uses the StringBuilder class to instantiate the strContent object in the getURLRaw() function. However, It doesnt help much. It still very slow. So What did I do wrong ? Any suggestion ?

Here is my Download Class
Any help would be appreciate :)

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.net.*;  
import java.io.*; 

public class Download
      {
	public String getURLRaw(String strURL)
         {
            StringBuilder strContent = new StringBuilder(strURL);
            String strContent="";  
            try
            {
URL myWebAddress= new URL(strURL);
             
               URLConnection myConn = myWebAddress.openConnection(); 
             
               InputStream myStrIn = myConn.getInputStream();
              
               BufferedReader myInputFile = new BufferedReader(new InputStreamReader(myStrIn));
               strContent="";
               
               for(String strLine = null; (strLine = myInputFile.readLine()) !=null;)
            {
               
               strContent += strLine + "\r\n";
            }
         }
            catch (MalformedURLException errnum)
               {
              
               System.out.println("Error");
               }
            catch (IOException errnum)
               {
               
               System.out.println("Error");
               }
               
               return strContent;
         }

Well for starters, it's a common misconception that a line like

string += "new stuff";

Is innocent when thinking about efficiency - however it is extremely inefficient because Strings are considered final objects at runtime in java. What java actually does with this line is instantiate a temp object called StringBuffer, which then writes your string + "new stuff", then converts it back to a string and returns the new string.

As you may imagine - this is quite expensive with huge numbers of appends to a string. A more efficient and faster way of approach is the following:

StringBuffer strContent = new StringBuffer();
[i]...[/i] 
strContent.append(strLine + "\r\n");
[i]...[/i]
return strContent.toString();

Hey Anyday, Thank for reply
I replace the strContent.append(strLine + "\r\n"); to line 26 and then compile it. However, the message shows
cannot find symbol
strContent.append(strLine + "\r\n");
^
symbol: method append(String)

You didn't instantiate strContent as a StringBuffer - and you had strContent instantiated as both a String and StringBuilder object which is incorrect. I've bolded the specific changes needed in the method.

public String getURLRaw(String strURL)
         {
            [b]StringBuffer strContent = new StringBuffer(); [/b] 
            try
            {
               URL myWebAddress= new URL(strURL);
 
               URLConnection myConn = myWebAddress.openConnection(); 
 
               InputStream myStrIn = myConn.getInputStream();
 
               BufferedReader myInputFile = new BufferedReader(new InputStreamReader(myStrIn));

               for(String strLine = null; (strLine = myInputFile.readLine()) !=null;)
               {
                  [b]strContent.append(strLine + "\r\n");[/b]
               }
            }
            catch (MalformedURLException errnum)
            {
                System.out.println("Error");
            }
            catch (IOException errnum)
            {
                System.out.println("Error");
            }
 
            [b]return strContent.toString();[/b]
         }
commented: that worked for me now. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.