how to access a file from within an applet???

P

Prem G

Hi,

I have written a public class, lets call it class1. Within this class,
I have a method (method1) which accesses a file, retrieves a bit of
information from the file, and then returns a vector as output.

If I try to call method1 from any normal class, it works fine and I
get the resultant vector. However, when I try to do the same thing
from within an applet class...

e.g. public class class2 extends Applet {

public void init() {
....
}

public void paint(Graphics g) {
....
}
}

if I try to call method1 from within the paint method above in class2,
it just does not work. Does anyone have any idea why this method would
not work within the paint method above and still work within classes
that do not extend Applet?

Also, is there any good way of debugging the java when it gives
runtime errors within applets? In this case for example, I can view
the applet but when method1 is called, the applet crashes. I know
about using Graphics.drawString("sometext") but in cases such as this,
drawString is not very useful.
 
C

Christophe Vanfleteren

Prem said:
Hi,

I have written a public class, lets call it class1. Within this class,
I have a method (method1) which accesses a file, retrieves a bit of
information from the file, and then returns a vector as output.

If I try to call method1 from any normal class, it works fine and I
get the resultant vector. However, when I try to do the same thing
from within an applet class...

e.g. public class class2 extends Applet {

public void init() {
....
}

public void paint(Graphics g) {
....
}
}

if I try to call method1 from within the paint method above in class2,
it just does not work. Does anyone have any idea why this method would
not work within the paint method above and still work within classes
that do not extend Applet?

Also, is there any good way of debugging the java when it gives
runtime errors within applets? In this case for example, I can view
the applet but when method1 is called, the applet crashes. I know
about using Graphics.drawString("sometext") but in cases such as this,
drawString is not very useful.

Lots of things wrong with this:

first of all: accessing the filesystem is a restricted operation in an
applet, and you won't be allowed to do it unless your applet is signed.

Second, do not read a file from within your paint method. It is only meant
to paint the applet, and could be called several times. The init method
would be a better place to do this.

As for debugging, you can still print to System.out and System.err, and you
can see this if you activate the java console.
 
E

e.Morr

Since the applet runs in a browser or sandbox, you cannot access the
client's file system. This is intentional applet behavior. If you're trying
for a file server side, I don't believe you can do that, either.

ed
 
P

Prem G

Thank you for the advice. Actually, I managed to find a way of
accessing files for read via java.net. I can grant the file
permissions of (777 - read/write/execute for all) and then access the
file via a URL connection to the file's URL.

Yes, I definitely should be doing most of my programming in the init()
method.

Also, I agree that the way I've done it means that it is not secure
since anyone can read this file. However, the applet is part of a User
Interface and so a signed applet may not be that "user friendly" in my
opinion.

Thanks again for the help! Hope I enlightened you back if you were not
aware that there was a way to access files via URLs.

Cheers,
Prem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top