Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Thumbs up [SOLVED] Downloading source code of specified URL

    Hi everybody,

    I'm trying to download a web page.But the problem is, the downloaded code contains repetition of some part of the code after the end of file. Here is the program......

    Anyone please help me to solve this issue......

    public void download(String source, String destination) throws MalformedURLException,IOException
    {
    BufferedInputStream in = new BufferedInputStream(new java.net.URL(source).openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    BufferedOutputStream bout = new BufferedOutputStream(fos,1024);

    byte data[] = new byte[1024];

    while(in.read(data)!=-1)
    {
    bout.write(data);
    }

    bout.close();
    fos.close();
    in.close();
    }
    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The last read is unlikely to fill the byte buffer, yet you write out the entire buffer including the end section which hasn't been overwritten. This is why InputStream.read() returns an integer; use it.

    Also, fos.close() is unnecessary as bout.close() has already closed it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks for rly,

    I couldn't get u correct. Can u tell me clearly?

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You have a buffer called data[].
    You read bytes into this buffer.
    Sometimes you won't fill this buffer.
    This happens especially at the end of a stream.
    When this happens, the end of the buffer still contains the old data.
    You then write the old data to bout.
    Using the return value from InputStream.read() will solve the problem.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. wrong source code
    By pro85 in forum Java Applets
    Replies: 4
    Last Post: 02-17-2009, 03:46 AM
  2. help with loops with source code
    By bmxriderss in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 09:56 AM
  3. Replies: 1
    Last Post: 11-28-2008, 06:27 PM
  4. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  5. Need a source code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •