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

Discussion in 'Java' started by Prem G, Apr 3, 2004.

  1. Prem G

    Prem G Guest

    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.
    Prem G, Apr 3, 2004
    #1
    1. Advertising

  2. Prem G wrote:

    > 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.

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Apr 3, 2004
    #2
    1. Advertising

  3. Prem G

    e.Morr Guest

    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


    "Prem G" <> wrote in message
    news:...
    > 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.
    e.Morr, Apr 3, 2004
    #3
  4. Prem G

    Prem G Guest

    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.
    Prem G, Apr 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    616
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    850
    webster
    Jul 20, 2003
  3. Lukasz Borcz

    pdf file within java applet - tutorial

    Lukasz Borcz, Feb 5, 2004, in forum: Java
    Replies:
    11
    Views:
    10,660
    markee174
    Aug 25, 2010
  4. Krista
    Replies:
    3
    Views:
    2,311
    Andrew Thompson
    Sep 15, 2004
  5. Replies:
    1
    Views:
    545
    Andrew Thompson
    Nov 19, 2007
Loading...

Share This Page