Results 1 to 5 of 5
- 05-07-2009, 03:04 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
[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();
}
}
- 05-07-2009, 03:10 PM #2
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
- 05-07-2009, 03:19 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
Thanks for rly,
I couldn't get u correct. Can u tell me clearly?
- 05-07-2009, 03:27 PM #4
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
- 05-08-2009, 11:08 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
wrong source code
By pro85 in forum Java AppletsReplies: 4Last Post: 02-17-2009, 03:46 AM -
help with loops with source code
By bmxriderss in forum New To JavaReplies: 2Last Post: 01-12-2009, 09:56 AM -
MavenJava - browse source code of all open source projects online
By jirkacelak in forum Java SoftwareReplies: 1Last Post: 11-28-2008, 06:27 PM -
Help me out in compiling the source code
By aks.nitw in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 08:33 AM -
Need a source code
By vissu007 in forum New To JavaReplies: 1Last Post: 07-05-2007, 07:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks