how to use File in applet?

I've got the html file like this.

<html>

<body>

<applet code=src.HelloWorld.class

archive ="Hello.jar"

width="800" height="500" >

</applet>

</body>

</html>

everthing is in the jar file. there is a button in applet, when user click it the applet is suppose to read a file 'this.f=new File("src/hello.txt");' the src folder is in that jar file.

when I run it the java console will report "java.security.AccessControlException: access denied (java.io.FilePermission src\hello.txt read)

"

what can i do with that?

thanks

[641 byte] By [JbloodHa] at [2007-10-3 4:52:26]
# 1
Files relate to your filesystem, not to the classpath.Your Jar contents are resources, which can be accessed via the Class.getResource() method - take a look at that in the API.
itchyscratchya at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...
# 2
thanks itchyscratchy,I've tried Class.getResource() like this File f=new File(getClass().getResource("hello.txt") );it will throw an exception, because File does not support File(URL) constructor
JbloodHa at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...
# 3
and the wired thing is, I can run this applet perfectly on my machine but when it run on the online server, it always says java.security.AccessControlException: access denied (java.io.FilePermission...
JbloodHa at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...
# 4
it will throw an exception, because File does not support File(URL) constructorCorrect. As I said, File objects relate to the filesystem. You do not want to use them.The getResource() method gives you a URL. Open a stream from it and read the resource that way.
itchyscratchya at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...
# 5
The security exception is due to the applet security sandbox. You're (erroneously) trying te get it to read from the local filesystem, which an unsigned applet is not allowed to do.Worth doing a quick bit of background reading on what unsigned applets can and can't do...
itchyscratchya at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...
# 6

You can't write to a file using an applet unles you sign or use a server/cgi something like that.

You CAN read a file on the server side of an applet:

String file = "fileName.txt";

URL fileURL = new URL(applet.getCodeBase(), file);

BufferedInputSream steam = new BufferedInputStream(fileURL.openStream()));

and then proceed as you normally would.

Message was edited by:

galaxyy

galaxyya at 2007-7-14 22:57:13 > top of Java-index,Desktop,Core GUI APIs...