Hello All,
I’m not totally new to Java. I’ve been messing around with it for the past couple of years.
Not until the past couple of months have I decided to become serious about it.
Which leads me to my question. I’ve mostly only done stuff with graphics and some controls here and there, but now I have come across an apllication where I need to read HTML, perform operations on it, and store it in a text box or something where I can copy it to the clipboard. However, I am stuck on step 1 - actually reading the file.
Here is my code thus far in applet form:
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.lang.Math;
import java.util.Date;
import java.util.Random;
import java.awt.event.*;
import java.applet.AudioClip;
public class myAppletIO extends java.applet.Applet {
URL myURL;
URL url;
URLConnection urlconnection;
InputStream myIS;
String yep = "wrtjwhrtkj:";
public void init() {
String s;
setBackground(Color.yellow);
setForeground(Color.blue);
yep.concat("Hello!");
String inputLine;
DataInputStream inStream;
URLConnection connection;
//Step 1&2: Create URL and URLConnection instances
try{
url = new URL("www.yahoo.com");
}catch(MalformedURLException e) {yep.concat("Can't access URL");
/// yep.concat("String: " + e.toString());
System.out.println("String: " + e.toString());
// yep.concat("Message: " + e.getMessage());
System.out.println("Message: " + e.getMessage());
repaint();}
/*
try{
myURLConnection = url.openConnection();//
}catch(IOException e) {System.out.println("Can't open connection to URL");
yep.concat("Can't open connection to URL"); repaint();}
*/
//Step 3: Get InputStream associated with URLConnection
/* try {
myIS = myURLConnection.getInputStream();
}
catch(IOException e) {System.out.println("Can't open connection to URL");}
*/
//STEP A
// InputStreamReader myISR = new InputStreamReader(myIS);
//STEP B
// BufferedReader BR = new BufferedReader(myISR);
/* for(int i=0; i<3; i++)
{
try {
s = BR.readLine();
System.out.println("Line " + i + ": " + s);
}catch(IOException e) {System.out.println("Line " + i + ": " + s);}
}
repaint();
//Close all streams
try{
BR.close();
myISR.close();
myIS.close();
} catch(IOException e) {yep.concat("can't close streams"); }
*/}
public void paint(Graphics g) {
g.drawString(yep,60,100);
}
}
I guess my first question would be: Can this be done with an applet?
As is, the applet compiles and runs successfully. However, when the second and / or third try blocks are uncommented, the code compiles successfully, however when it is run, the applet won’t init or I get the applet area box with the red X in it.
I also tried an application thinking that this might be an applet limitation thing (this is an example from a book):
import java.io.*;
import java.net.*;
import java.util.Date;
class URLConnections1
{
public static void main(String args[]) throws Exception
{
int character;
URL url = new URL("www.yahoo.com");
URLConnection urlconnection = url.openConnection();
System.out.println("Content type: " +
urlconnection.getContentType());
System.out.println("Document date: " +
new Date(urlconnection.getDate()));
System.out.println("Last modified: " +
new Date(urlconnection.getLastModified()));
System.out.println("Document expires: " +
urlconnection.getExpiration());
int contentlength = urlconnection.getContentLength();
System.out.println("Content length: " + contentlength);
if (contentlength > 0) {
InputStream in = urlconnection.getInputStream();
while ((character = in.read()) != -1) {
System.out.print((char) character);
}
in.close();
}
}
}
However, when I run this after I compile it, I get a [quote]"Exception in thread “main” java.lang.NoCclassDefFoundError: URLConnections1
[/quote]
The java version I’m using is 1.4.0 and sometimes 1.4.1. I’ve looked at many examples and consulted the book I’ve been studying from, Java 2 Black Book (which the app example came from), but I just can’t seem to get past this.
If anybody would have any suggestions or input, I’d really appreciate it.
Thanks,
Rick


I work 3rd shift. Yuck!