VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 12345678[9] ]


[ Next Thread | Previous Thread | Next Message | Previous Message ]

Date Posted: 20:24:42 05/28/03 Wed
Author: Tim
Subject: Here's the "reader" code:
In reply to: Tim 's message, "I'll give it a try!" on 20:22:52 05/28/03 Wed

Public domain:


import java.net.*;
import java.io.*;

public class WebReader
{

/********/
/*FIELDS*/
/********/

private URL url;
private StringBuffer out;

/***********/
/*ACCESSORS*/
/***********/

public URL getUrl()
{
return url;
}

public void setUrl(URL url)
{
this.url = url;
}

public StringBuffer getOut()
{
return out;
}

public void setOut(StringBuffer out)
{
this.out = out;
}

/****************/
/*HELPER METHODS*/
/****************/

public void read()
{
//declare streams
InputStream in = null;
BufferedInputStream bin = null;
DataInputStream din = null;

//create streams
try
{
in = getUrl().openStream();
bin = new BufferedInputStream(in);
din = new DataInputStream(bin);
System.out.println("Streams created");
String line;
while( (line = din.readLine()) != null )
{
getOut().append(line).append('\n');
}
System.out.println("Lines all read");
}
catch(IOException ioe)
{
System.err.println(ioe);
}
finally
{
//kill streams, no matter what happened
try
{
if(din != null)
{
din.close();
din = null;
}
System.out.println("\"din\" is closed");
if(bin != null)
{
bin.close();
bin = null;
}
System.out.println("\"bin\" is closed");
if(in != null)
{
in.close();
in = null;
}
System.out.println("\"in\" is closed");
}
catch(IOException ioe)
{
System.err.println(ioe);
}
}
}

/**************/
/*CONSTRUCTORS*/
/**************/

public WebReader(URL url)
{
setUrl(url);
setOut(new StringBuffer());
}

/*********/
/*STATICS*/
/*********/

public static String readFromWeb(URL url)
{
WebReader wr = new WebReader(url);
wr.read();
return wr.getOut().toString();
}
}


[ Next Thread | Previous Thread | Next Message | Previous Message ]


Forum timezone: GMT-5
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.