Problem reading from a URL...

Hi all...

I've been reading throught the sun's toturial on networking that you can find here:

http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html

this toturial provides a java program that reads from a URL. I've done the same exact thing in a JApplet but I am getting an IOException. That is, I copied the code provided and pasted it in a method within my applet but it didn't work.

any ideas?

thanks

[465 byte] By [Raieda] at [2007-11-27 10:32:42]
# 1

Read this: http://java.sun.com/sfaq/

~

yawmarka at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 2

look up "signed applet"

bsampieria at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 3

"Can applets read or write files?

In Java-enabled browsers, untrusted applets cannot read or write files at all. By default, downloaded applets are considered untrusted. There are two ways for an applet to be considered trusted: "

"The applet is installed on the local hard disk, in a directory on the CLASSPATH used by the program that you are using to run the applet. Usually, this is a Java-enabled browser, but it could be the appletviewer, or other Java programs that know how to load applets. "

"Sun's appletviewer allows applets to read files that reside in directories on the access control lists. "

if this means that I should put my applet and my file to be read in the same directory on my webstie, I already did and its giving me an Exception. if it means something else. Please clarify :)

thanks in advance

Raieda at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 4

(sigh) The very first sentence of the very first answer in the document that yawmark linked to:

"In general, applets loaded over the net are prevented from reading and writing files on the client file system, and from making network connections except to the originating host."

Hopefully this is obvious to you, but your code is trying to make a network connection.

DrClapa at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 5

thanks for your response...

yes, but my code is trying to open a network connection to the originating host and I am still getting the Exception !

regards,

Raieda at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> thanks for your response...

>

> yes, but my code is trying to open a network

> connection to the originating host and I am still

> getting the Exception !

>

> regards,

Since we have no view of the exception and no view of your code what can we say?

sabre150a at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 7

I just used the code that the toturial provided.

<code>

public String readCountry(String fileName) {

String s = "aaa";

try{

URL url = new URL("www.seoproducts.net/b/test.txt");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

while ((s = in.readLine()) != null)

System.out.println(s);

in.close();

} catch (Exception e){

System.out.println("Exception");

}

return s;

}

</code>

this is the method that causes the exception. This method is in an applet which is in an html page that is located in www.seoproducts.net/b where the test.txt file is also located and I am still getting the exception.

regards

Raieda at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 8

WHAT EXCEPTION?

ejpa at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 9

The error I am getting is:

no protocol: www.seoproducts.net/b/test.txt

Raieda at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 10

URL url = new URL(" http://www.seoproducts.net/b/test.txt");

Protocols are things like http, https, etc...

BigDaddyLoveHandlesa at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 11

Did the forum hiccup?

URL url = new URL(" http://www.seoproducts.net/b/test.txt");

URLs need protocols like http or https.

BigDaddyLoveHandlesa at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...
# 12

Thanks this worked :)

Raieda at 2007-7-28 18:18:17 > top of Java-index,Java Essentials,Java Programming...