I should point out that I didn't write the code, it was from the "Java Specialists Newsletter". It's an excellant source of java info and well worth subscribing too. The newsletter archive is here: http://www.javaspecialists.co.za/archive/archive.html
Russell > > Message: 1 > Date: Tue, 22 Jul 2003 09:34:35 -0700 > From: "DeAngelo Lampkin" <[EMAIL PROTECTED]> > Subject: RE: [Biojava-l] not biojava... > To: "Russell Smithies" <[EMAIL PROTECTED]>, > "[EMAIL PROTECTED] Org" <[EMAIL PROTECTED]> > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="Windows-1252" > > Very useful indeed. Perhaps we should think about an org.biojava.Dilbert package heirarchy? :) > > -----Original Message----- > From: Russell Smithies [mailto:[EMAIL PROTECTED] > Sent: Friday, July 18, 2003 4:12 PM > To: [EMAIL PROTECTED] Org > Subject: [Biojava-l] not biojava... > > > ...... but a useful peice of code everyone should have > ;-) > > --------------------------------------------------------- > import java.io.*; > import java.net.URL; > import javax.swing.*; > > public class DilbertComic { > > public static String todaysDilbert() throws IOException { > // open up the webpage to today's comic > URL url = new URL("http://www.dilbert.com"); > BufferedReader webRead = new BufferedReader( > new InputStreamReader(url.openStream())); > String line; > while ((line = webRead.readLine()) != null) { > if (line.indexOf("ALT=\"Today's Dilbert Comic\"") != -1) { > int offset = line.indexOf( > "<IMG SRC=\"/comics/dilbert/archive/images/dilbert"); > line = line.substring(offset + 10); > return "http://www.dilbert.com" + > line.substring(0, line.indexOf('"')); > } > } > return null; > } > > public static void download(URL url, File file) throws IOException { > InputStream in = url.openStream(); > FileOutputStream out = new FileOutputStream(file); > byte[] b = new byte[1024]; > int len; > while((len = in.read(b)) != -1) { > out.write(b, 0, len); > } > out.close(); > } > > public static void main(String[] args) throws IOException { > System.out.println("Looking for today's dilbert comic . . ."); > String today = todaysDilbert(); > > if (today == null) { > System.out.println("Could not find today's dilbert comic!"); > } else { > System.out.println("Found today's dilbert: " + today); > URL url = new URL(today); > > // download it as an ImageIcon and show it in a JFrame. > System.out.println("Downloading the Image . . ."); > ImageIcon im = new ImageIcon(url); > System.out.println("Downloaded the Image"); > > JFrame f = new JFrame("Today's Dilbert"); > f.getContentPane().add(new JLabel(im)); > f.pack(); > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > f.show(); > } > } > } > --------------------------------------------------------------- > > > _______________________________________________ > Biojava-l mailing list - [EMAIL PROTECTED] > http://biojava.org/mailman/listinfo/biojava-l > > > ------------------------------ > > _______________________________________________ > Biojava-l mailing list - [EMAIL PROTECTED] > http://biojava.org/mailman/listinfo/biojava-l > > > End of Biojava-l Digest, Vol 7, Issue 23 > **************************************** > _______________________________________________ Biojava-l mailing list - [EMAIL PROTECTED] http://biojava.org/mailman/listinfo/biojava-l