Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frustrated Newbie. Cannot read file in Applet 1

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
This must be something very fundamental, and I appreciate any guidance.
I've written an Applet using "BlueJ" that runs fine in it's AppletViewer, but cannot be run in a browser. My applet reads a text file, which is located in the same directory.
I've researched this to tears and can't get my head around the cause. I've tried putting everything onto a webserver, created and signed the JAR file, etc. From what I've read, the Applet should be able to read a file from the same directory without going through the jarsigner steps.

Here's a snippet of the code
Code:
    private void loadFile()
    {
        Frame parent = new Frame();
        File partFile = new File("Participants_R2.txt");
        if (partFile == null)
            return;
        map = new TreeMap<Integer, Entrant>();
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(partFile));
            String strLine = "";
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;

            while ((strLine = br.readLine()) != null)
            {
                lineNumber++;
                st = new StringTokenizer(strLine, ",");
                tokenNumber = 3;
                Entrant = stripQuotes(st.nextToken());
                Firm = stripQuotes(st.nextToken());
                blank = st.nextToken();
                while(st.hasMoreTokens())
                {
                    tokenNumber++;
                    pick[tokenNumber] = stripQuotes(st.nextToken());
                }
            }
         }
        catch(IOException e)
        {
         JOptionPane.showMessageDialog(parent, 
                "I/O error in file\n\n     " +
                        partFile.getName() +
                        "\n\nThis program will close", 
                "I/O Error", 
                JOptionPane.ERROR_MESSAGE);
                System.exit(1);
        }
    }

And the corresponding error:

java.security.AccessControlException: access denied (java.io.FilePermission 2009Participants_R2.txt read)
at java.security.AccessControlContext.checkPermission(Unknown Source)


Thanks in advance...


"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
More information on java policy file here.

That's the easiest way to test your applet, but keep in mind that may not be suitable for delivery if you don't have control over the client machines

Cheers,
Dian
 
Thanks Prosper and Dian. I'll give the information in the links a try later today. I will have no control over the Client machines. The file that I want the applet to read will reside on the Webserver in the same directory as my Java programs. Could my problem be that I simply don't have the "Participants_R2.txt" specified correctly? Does that need to be formed in a way that the Clients can see? (i.e. //123.45.67.89/Participants_R2.txt).

I read through a few tutorials about Policy, and they all seemed to be reliant on local PC settings.

In my application, it will read a fairly static text file to provide information for display. I might change that file weekly, but anybody who runs the applet will be reading that same file with current info. Does that help explain the scenario?

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Oh, I didn't understand the question, thought the file was local. To read a remote file I can think on two ways:
- Reading it through a http connection. You would need to sign the applet to do that, though
- Including the file in the applet jar and reading it from resource bundle, but I'm not sure if the changes would be reflected, as the JVM has a cache

Cheers,
Dian
 
I managed to get past the error using this hunk of code to handle the file IO:
Code:
        {
            URL             url;
            URLConnection   urlConn;
            DataInputStream dis;
            
            url = new URL(getCodeBase().toString() + "/MarchMad/2009Participants_R2.txt");
            urlConn = url.openConnection();
            urlConn.setDoInput(true);
            urlConn.setUseCaches(false);
            
            dis = new DataInputStream(urlConn.getInputStream());
            String strLine = "";
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;
            while ((strLine = dis.readLine()) != null)

(etc) (same as before)

My new problem is that the screen which appears in the browser is too small to fit all the information. Is that a default I can control? When I build the screen, all my JLabels default to "[ No Info }", but once the data is read from the file, that text is replaced with strings which can be 20-30 characters long.

Thanks again for the help. The HTTP connection advice really helped.

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top