Re: BufferedReader do not change output for new file
- From: IchBin <weconsultants@xxxxxxxxx>
- Date: Thu, 26 Jul 2007 16:22:29 -0400
Roedy Green wrote:
On Wed, 25 Jul 2007 23:23:44 -0700, gupta.divyendu@xxxxxxxxx wrote,
quoted or indirectly quoted someone who said :
yahoo = new URL(m);
here is a more traditional way to skin that cat:
// Read encoded chars from a buffered URL.
// WARNING! unsigned Applets may only read from the server they were
loaded from.
// Sockets are a low level tool. For HTTP you will see all the headers
and length bytes.
// For HTTP normally you want the HttpURLConnection rather than
Socket.
// To copy/download files see
http://mindprod.com/products.html#FILETRANSFER.
// To GET/PUT to a server see http://mindprod.com/products.html#HTTP
for a l/ibrary of useful methods.
// import java.io.*;
// import java.net.*;
// O P E N
// Generate an HTTP GET Command
URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" );
URLConnection urlc = (URLConnection)url.openConnection();
urlc.setRequestMethod("GET");
// Identify as Firefox 1.5
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;
rv:1.8) Gecko/20051111 Firefox/1.5";
urlc.setRequestProperty("User-Agent", userAgent );
// optionally turn off keep-alive
urlc.setRequestProperty( "Connection", "close" );
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );
urlc.setUseCaches( false );
urlc.connect();
long length = urlc.getContentLength(); // -1 if not available
// if you want to analyse the URL
String protocol = url.getProtocol(); // http
String host = url.getHost(); // www.billabong.com
String file = url.getFile(); // lyrics.txt
int port = url.getPort();// 80
InputStream is = urlc.getInputStream();
InputStreamReader eisr = new InputStreamReader( is,"UTF-8" );
// usually UTF-8 for Unicode 8-bit giving the full Unicode set, no
BOMs.
// Cp437 is IBM OEM.
// See "encoding" in the Java glossary for alternatives
// such as UTF-16BE for 16-bit, big-endian Unicode characters.
BufferedReader br = new BufferedReader( eisr, 4096 /* buffsize */ );
// R E A D
char[] ca = new char[1024];
// -1 means eof.
// You don't necessarily get all you ask for in one read.
// You get what's immediately available.
int charsRead = br.read( ca );
String line;
// File being read need not have have a terminal \n.
// File being read may safely use any mixture of \r\n, \r or \n line
terminators.
line = br.readLine();
// line == null means EOF
int aChar;
aChar = br.read();
// aChar == -1 means EOF
// C L O S E
br.close();
Sorry Roedy but should this example code be using HttpURLConnection and not URLConnection to use the setRequestMethod(String) method alone with the associated exceptions Try\Catch block?
--
Thanks in Advance... http://weconsulting.org
IchBin, Philadelphia, Pa, USA http://ichbinquotations.weconsulting.org
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
.
- Follow-Ups:
- Re: BufferedReader do not change output for new file
- From: Roedy Green
- Re: BufferedReader do not change output for new file
- References:
- BufferedReader do not change output for new file
- From: gupta . divyendu
- Re: BufferedReader do not change output for new file
- From: Roedy Green
- BufferedReader do not change output for new file
- Prev by Date: Re: Log4J Ignores log4j.LogLevel
- Next by Date: Re: Log4J Ignores log4j.LogLevel
- Previous by thread: Re: BufferedReader do not change output for new file
- Next by thread: Re: BufferedReader do not change output for new file
- Index(es):