Saturday, February 27, 2010

Reading data from Internet

This URL seems to be pretty useful for this purpose.

http://java.sys-con.com/node/39248

Always use BufferedReader while reading InputStreams. Not using BufferedReader would lead to corrupt data and may not give correct results.

Code snippet is as given below :


HttpURLConnection hpCon;
hp = new URL(url);
hpCon = (HttpURLConnection)hp.openConnection();
InputStream in = hpCon.getInputStream();
InputStreamReader inStream = new InputStreamReader(in);
BufferedReader input = new BufferedReader(inStream);

String tempString="";
while(true)
{

tempString = input.readLine();
if(tempString == null) break;
output.append(tempString);
}
hp = null;
hpCon = null;

No comments:

Post a Comment